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

📄 maindlg.cpp

📁 激光加工控制系统 可以读入plt文件
💻 CPP
📖 第 1 页 / 共 4 页
字号:
// maindlg.cpp : implementation file
//

#include "stdafx.h"
#include "jgs.h"
#include "maindlg.h"
#include "math.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "process.h"
#include "malloc.h"

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

#define NUM_OF_CIRS 200000L
#define MINUTES 0
#define SECONDS 1
#define PI 3.1415926535898
#define X_RATE 1
#define Y_RATE 1

typedef struct{
  int order,value;
}code_strt;

/////////////////////////////////////////////////////////////////////////////
// maindlg dialog
put_a_line_to_tar_fpe(char *buf);
get_a_line(FILE *fpe);
load_mac_para(FILE *fpe);
putxy1(int i,int num);
putxy(int i,int num);
trans_line(int b_p,FILE *t_fpe);
trans_circ(int b_p,FILE *t_fpe,int type);
int put1();
int put2();
int cut(int n);
trans_plt_to_g_code();
trans_gfile_to_23_code();
trans_g_to_23_code();
trans_gfile_to_23();
trans_g_to_23();
trans_file(FILE *fr_file,FILE *to_file);
put_a_line(FILE *fpe,char *buf);
proc_line(int b_p,FILE *to_file);
proc_circ(int b_p,FILE *to_file,int type);
float count_angle(float x0, float y0, float x1, float y1);
recover_data();
set_data();
load_mac_para(FILE *fpe);
test_dog();

extern int flag;
extern int error_flag;
extern int quit_flag;

CString work_file;
BOOL dk_flag;

FILE *to_file;
FILE *tar_fpe;

long line_n;
int r_p;
int order1 = 0;
double x_ps=0,y_ps=0;
float xyrate;
int x,y;
char buf1[100],buf2[20];
char nbuf[200];
int a_buf[100];
int b_buf[100];
int acc[2]={4,4};
float pulses_per_unit[2];
int add_flag=1,sub_flag=1;
long bb_x,bb_y;
float vmax,b[2],e[2];
char buf[200];
float *xarr[NUM_OF_CIRS];
float *yarr[NUM_OF_CIRS];
int pen_order[NUM_OF_CIRS];
int end_arr;
int end_p[NUM_OF_CIRS];
float min_x[NUM_OF_CIRS],max_x[NUM_OF_CIRS],min_y[NUM_OF_CIRS],max_y[NUM_OF_CIRS];
int con[NUM_OF_CIRS][20];
int end_con[NUM_OF_CIRS];
int cur_order;
int order[NUM_OF_CIRS];
int cutted[NUM_OF_CIRS];
int spds;

float b_x,b_y,e_x,e_y,o_x,o_y;
code_strt wait[30];
code_strt output[30];
long x_count=0,y_count=0;
int x_count1=0,y_count1=0;
int end_wait=0,end_output=0;
char go_home_str[200];     // G28
char command_str[200];     // begin code
int board_no;
float pulse_rate;          // max pulse rate means max speed
float interval;            // time to count (us)
float xsteps;              // pulses per unit(G CODE)
float ysteps;
float zsteps;
int danwei;                // Minute or Second
float xmaxv,xmaxv2,xmaxv3;               // Max Speed units(G CODE) per danwei
float ymaxv,ymaxv2,ymaxv3;
float xmaxv1;               // Max Speed units(G CODE) per danwei
float ymaxv1;
float zmaxv,zmaxv2,zmaxv3;
float xmaxa,xmaxa2,xmaxa3;               // Max Acceleration
float ymaxa,ymaxa2,ymaxa3;
float zmaxa,zmaxa2,zmaxa3;
float xdspeed;            // Default Speed
float ydspeed;            // Default Speed
float zdspeed;
float max_accu_arc;        // circle accuracy
float xhl,xll,yhl,yll,zhl,zll;    // Max And Min Position

char filename[20];

int old_mode;
float oldx=0,oldy=0,oldi=0,oldj=0;
float x_once_max_a_pulses,x_once_max_v_pulses;
float y_once_max_a_pulses,y_once_max_v_pulses;
float x_once_max_a_pulses1,y_once_max_a_pulses1;
float cur_speed;
float xydspeed;
int begin_flag;
float x_len_to_acc,y_len_to_acc;
int x_times,y_times;
float x_adj=0,y_adj=0;
int mode=0;
int oldxx=0,oldyy=0;
int line_flag;
int trans_g_file_flag;
int first_flag;

maindlg::maindlg(CWnd* pParent /*=NULL*/)
	: CDialog(maindlg::IDD, pParent)
{
	long i;

	for (i=0;i<10;i++){
	  _asm {
	    mov dx,0x37a
	    mov al,0
	    out dx,al
		mov dx,0x378
	    mov al,0
		out dx,al
	  }
	}
    for (i=0;i<NUM_OF_CIRS;i++){
	  xarr[i] = yarr[i] = NULL;
	}
	//{{AFX_DATA_INIT(maindlg)
	m_work_name = _T("");
	m_work_status = _T("");
	m_dk = FALSE;
	//}}AFX_DATA_INIT
}


void maindlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(maindlg)
	DDX_Text(pDX, IDC_WORK_NAME, m_work_name);
	DDX_Text(pDX, IDC_WORK_STATUS, m_work_status);
	DDX_Check(pDX, IDC_dk, m_dk);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(maindlg, CDialog)
	//{{AFX_MSG_MAP(maindlg)
	ON_BN_CLICKED(IDC_RUN, OnRun)
	ON_BN_CLICKED(IDC_SET_FILE_NAME, OnSetFileName)
	ON_BN_CLICKED(IDC_SIMU, OnSimu)
	ON_BN_CLICKED(IDC_TRANS, OnTrans)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// maindlg message handlers

void maindlg::OnRun() 
{
	// TODO: Add your control notification handler code here
	int i,j;

			_asm {
				mov dx,0x37a
				mov al,0xff
				out dx,al
            }
    for (i=0;i<10000;i++)
		for (j=0;j<2000;j++);
	_spawnl(P_WAIT,"c:\\jgs\\s1.exe","c:\\jgs\\s1.exe",NULL);
			_asm {
				mov dx,0x37a
				mov al,0
				out dx,al
            }
}

void maindlg::OnSetFileName() 
{
	// TODO: Add your control notification handler code here
	CFileDialog fdlg(TRUE,NULL,NULL,0,"PLT Files (*.plt)|*.plt||",NULL);

	if (fdlg.DoModal() == IDOK){
		work_file = fdlg.GetPathName();
		SetDlgItemText(IDC_WORK_NAME,work_file.GetBuffer(200));
	    SetDlgItemText(IDC_WORK_STATUS,"就绪");
    }
}

void maindlg::OnSimu() 
{
	// TODO: Add your control notification handler code here
	system("c:\\jgs\\simu.exe");
}

void maindlg::OnTrans() 
{
	// TODO: Add your control notification handler code here

	dk_flag = IsDlgButtonChecked(IDC_dk);
    oldx = oldy = 0;
	x_ps=0;
	y_ps=0;
	SetDlgItemText(IDC_WORK_STATUS,"正在转换......");
    strcpy(filename,work_file.GetBuffer(200));
    if (filename[strlen(filename)-4] == '.'){
      trans_g_file_flag = 0;
 	  if (trans_plt_to_g_code() == -1)
	    SetDlgItemText(IDC_WORK_STATUS,"转换出错,可能文件不存在.");
	  else{
	    if (trans_g_to_23_code() == 1)
	      SetDlgItemText(IDC_WORK_STATUS,"转换完成.");
	    else
	      SetDlgItemText(IDC_WORK_STATUS,"转换出错,可能setup1.fil不存在.");
	  }
	}
	else{
	  trans_g_file_flag = 1;
      if (trans_gfile_to_23_code() == 1)
	      SetDlgItemText(IDC_WORK_STATUS,"转换完成,就绪");
	    else
	      SetDlgItemText(IDC_WORK_STATUS,"转换出错,可能setup1.fil不存在.");
    }
}



trans_plt_to_g_code()
{
  FILE *fpe;
  int ret,x1,y1,x2,y2,j,flag,m,n;
  long i;
  int cur_pen_order;
  float fx,fy;
  int spd[10];
  float minx,miny,maxx,maxy;
  float curx,cury,x,y,oldx,oldy;

  b_x = b_y = e_x = e_y = o_x = o_y = 0;
  x_count=0;
  y_count=0;
  x_count1=0;
  y_count1=0;
  end_wait=0;
  end_output=0;
  x_adj=0;
  y_adj=0;
  mode=0;
  oldxx=0;
  oldyy=0;
  for (i=0;i<NUM_OF_CIRS;i++){
	  xarr[i] = yarr[i] = NULL;
  }
  
  strcpy(filename,work_file.GetBuffer(200));
  fpe = fopen(filename,"rb");
  if (fpe == NULL){
	  return -1;
  }
  ret = get_a_line(fpe);
  minx = miny = maxx = maxy = curx = cury = 0;
  n = 0;
  while (ret != -1){
	  if ((buf[0] == 'V')&&(buf[1] == 'S')){
        spd[n++] = atoi(&(buf[2]))*50;
	  }
    if (buf[0] == 'P'){
      if ((buf[1] == 'U')||(buf[1] == 'D')){
	x = 1.25*atof(&(buf[2]))*X_RATE/10.;
	i = 2;
	while (buf[i] != ' ')
	  i ++;
	y = 2*atof(&(buf[i]))*Y_RATE/10.;
	curx = x;
	cury = y;
	if (minx > curx)
	  minx = curx;
	if (maxx < curx)
	  maxx = curx;
	if (miny > cury)
	  miny = cury;
	if (maxy < cury)
	  maxy = cury;
      }
    }
    ret = get_a_line(fpe);
  }
  fclose(fpe);

  fpe = fopen(filename,"rb");
  ret = get_a_line(fpe);
  oldx = oldy = 0;
  x1 = -1000;
  end_arr = -1;
  j = 0;
  flag = 0;
  while (ret != -1){
    if (buf[0] == 'P'){
      if ((buf[1] == 'U')||(buf[1] == 'D')){
	i = 2;
	while (buf[i] != ' ')
	  i ++;
	if (buf[1] == 'D'){
	  if (flag == 1){
	    flag = 0;
	  }
	}
	end_p[end_arr]++;
      }
    }
    if ((buf[0] == 'L')&&(buf[1] == 'T')){
	flag = 1;
	if (end_arr != -1){
	  end_arr ++;
	  end_p[end_arr] = 0;
	}
	else{
	  end_arr = 0;
	  end_p[end_arr] = 0;
	}
    }
    ret = get_a_line(fpe);
  }
  fclose(fpe);
  end_arr ++;
  end_p[end_arr] = 0;
  
  for (i=0;i<end_arr;i++){
	  xarr[i] = (float *)(malloc(sizeof(float)*end_p[i]));
	  if (xarr[i] == NULL){
		  for (j=0;j<i;j++){
			  free(xarr[j]);
			  free(yarr[j]);
          }
		  MessageBox(NULL,"对不起,图形太大,内存不够.",NULL,MB_OK);
		  exit(-1);
      }
	  yarr[i] = (float *)malloc(sizeof(float)*end_p[i]);
	  if (yarr[i] == NULL){
		  free(xarr[i]);
		  for (j=0;j<i;j++){
			  free(xarr[j]);
			  free(yarr[j]);
          }
		  MessageBox(NULL,"对不起,图形太大,内存不够.",NULL,MB_OK);
		  exit(-1);
      }
  }


  fpe = fopen(filename,"rb");
  ret = get_a_line(fpe);
  oldx = oldy = 0;
  x1 = -1000;
  end_arr = -1;
  j = 0;
  flag = 0;
  while (ret != -1){
    if (buf[0] == 'P'){
      if ((buf[1] == 'U')||(buf[1] == 'D')){
	x = 1.25*atof(&(buf[2]))*X_RATE/10.;
	i = 2;
	while (buf[i] != ' ')
	  i ++;
	y = 2*atof(&(buf[i]))*Y_RATE/10.;
	curx = x;
	cury = y;
	if (buf[1] == 'D'){
	  if (flag == 1){
	    flag = 0;
	  }
	}
	oldx = curx;
	oldy = cury;
	xarr[end_arr][end_p[end_arr]] = x;
	yarr[end_arr][end_p[end_arr]] = y;
	end_p[end_arr]++;
      pen_order[end_arr] = cur_pen_order;
      }
    }
    if ((buf[0] == 'S')&&(buf[1] == 'P')){
		cur_pen_order = atoi(&(buf[2]));
    }
    if ((buf[0] == 'L')&&(buf[1] == 'T')){
	flag = 1;
	if (end_arr != -1){
	  end_arr ++;
	  end_p[end_arr] = 0;
	}
	else{
	  end_arr = 0;
	  end_p[end_arr] = 0;
	}
    }
    ret = get_a_line(fpe);
  }
  fclose(fpe);
//  pen_order[end_arr] = cur_pen_order;
  end_arr ++;
  end_p[end_arr] = 0;


  for (i=0;i<end_arr;i++){
    min_x[i] = 1000000;
    max_x[i] = -1000000;
    min_y[i] = 1000000;
    max_y[i] = -1000000;
    for (j=0;j<end_p[i];j++){
      x = xarr[i][j];
      y = yarr[i][j];
      if (x < min_x[i])
	min_x[i] = x;
      if (x > max_x[i])
	max_x[i] = x;
      if (y < min_y[i])
	min_y[i] = y;
      if (y > max_y[i])
	max_y[i] = y;
    }
  }
  for (i=0;i<end_arr;i++){
    end_con[i] = 0;
    for (j=0;j<end_arr;j++){
      if (i != j){
	if ((min_x[j] >= (min_x[i]+1))&&(max_x[j] <= (max_x[i]-1))&&
	    (min_y[j] >= (min_y[i]+1))&&(max_y[j] <= (max_y[i]-1))){
	  con[i][end_con[i]] = j;
	  end_con[i] ++;
	}
      }
    }
  }

  for (i=0;i<end_arr;i++){
    for (j=0;j<end_con[i];j++)
      printf("%d ",con[i][j]);
    printf("\n");
  }

  cur_order = 0;
  for (i=0;i<end_arr;i++)
    cutted[i] = 0;
  if (end_arr < 10000){
    flag = 0;
    j = 0;
    while (flag == 0){
      cut(j);
      j ++;
      flag = 1;
      for (i=0;i<end_arr;i++)
        if (cutted[i] == 0)
	  flag = 0;
	}
    for (i=0;i<end_arr;i++){
      for (j=0;j<end_arr;j++){
        if (order[j] == i){
	      for (m=0;m<end_p[j];m++){
	        curx = xarr[j][m];
	        cury = yarr[j][m];
		  }
        }
	  }
    }
  }
  else{
    for (j=0;j<end_arr;j++){
	  for (m=0;m<end_p[j];m++){
	    curx = xarr[j][m];
	    cury = yarr[j][m];
	  }
    }
  }

  fpe = fopen("c:\\jgs\\test.g","w");
  fprintf(fpe,"G92X0Y0\n");
  fprintf(fpe,"\n");
  fprintf(fpe,"M9\n");
  cur_pen_order = -1;
  if (end_arr < 10000){
  for (i=0;i<end_arr;i++){
    for (j=0;j<end_arr;j++){
      if (order[j] == i){
	for (m=0;m<end_p[j];m++){
	  fx = xarr[j][m];
	  fy = yarr[j][m];
//	  fx = (fx+200.)/10.;
//	  fy = (fy+150.)/10.;
	  fx = fx/10.;
	  fy = fy/10.;
	  if (m != 0){
	    fprintf(fpe,"G01X%fY%f\n",fx,fy);
	  }
	  else{
	    fprintf(fpe,"G00X%fY%f\n",fx,fy);
		if (cur_pen_order != pen_order[j]){
          fprintf(fpe,"F");
          fprintf(fpe,"%d\n",spd[pen_order[j]-1]);
	      fprintf(fpe,"M%d\n",pen_order[j]-1);
        }
		cur_pen_order = pen_order[j];
	    fprintf(fpe,"M8\n");
	  }
	}
	fprintf(fpe,"M9\n");
      }
    }
  }
  }
  else{
    for (j=0;j<end_arr;j++){
	  for (m=0;m<end_p[j];m++){
	    fx = xarr[j][m];
	    fy = yarr[j][m];
	    fx = fx/10.;
	    fy = fy/10.;
	    if (m != 0){
	      fprintf(fpe,"G01X%fY%f\n",fx,fy);
		}
	    else{
	      fprintf(fpe,"G00X%fY%f\n",fx,fy);
		  if (cur_pen_order != pen_order[j]){
            fprintf(fpe,"F");
            fprintf(fpe,"%d\n",spd[pen_order[j]-1]);
	        fprintf(fpe,"M%d\n",pen_order[j]-1);
		  }
		  cur_pen_order = pen_order[j];
	      fprintf(fpe,"M8\n");
		}
	  }
	  fprintf(fpe,"M9\n");
	}
  }

  fprintf(fpe,"G00X0Y0\n");
  fclose(fpe);

⌨️ 快捷键说明

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