📄 maindlg1011.cpp
字号:
// 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
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);
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 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);
extern int flag;
CString work_file;
BOOL dk_flag;
FILE *to_file;
FILE *tar_fpe;
int order1 = 0;
long x_ps=0,y_ps=0;
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];
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 xydspeed; // 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;
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<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,NULL,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 = atof(&(buf[2]))/10.;
i = 2;
while (buf[i] != ' ')
i ++;
y = atof(&(buf[i]))/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 = atof(&(buf[2]))/10.;
i = 2;
while (buf[i] != ' ')
i ++;
y = atof(&(buf[i]))/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]++;
}
}
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){
pen_order[end_arr] = cur_pen_order;
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+6000.)/10.;
fy = (fy+4500.)/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+6000.)/10.;
fy = (fy+4500.)/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);
for (i=0;i<end_arr;i++){
if (xarr[i] != NULL)
free(xarr[i]);
if (yarr[i] != NULL)
free(yarr[i]);
}
return 1;
}
int cut(int n)
{
int k;
if (cutted[n] == 1)
return 1;
if (end_con[n] == 0){
order[n] = cur_order;
cur_order ++;
cutted[n] = 1;
return 1;
}
for (k=0;k<end_con[n];k++)
cut(con[n][k]);
order[n] = cur_order;
cur_order ++;
cutted[n] = 1;
return 1;
}
trans_g_to_23_code()
{
FILE *fp;
int i,times,ch;
fp = fopen("c:\\jgs\\setup1.fil","rb");
if (fp == NULL){
return -1;
}
load_mac_para(fp);
xmaxv1 = xmaxv; // Max Speed units(G CODE) per danwei
ymaxv1 = ymaxv;
x_once_max_a_pulses = xmaxa*xsteps*interval/1000.0;
y_once_max_a_pulses = ymaxa*ysteps*interval/1000.0;
x_once_max_a_pulses /= (1000/interval);
y_once_max_a_pulses /= (1000/interval);
x_once_max_v_pulses = xmaxv*xsteps*interval/1000.0;
y_once_max_v_pulses = ymaxv*ysteps*interval/1000.0;
if (danwei == MINUTES){
x_once_max_a_pulses /= 60.0;
y_once_max_a_pulses /= 60.0;
x_once_max_v_pulses /= 60.0;
y_once_max_v_pulses /= 60.0;
}
x_times = x_once_max_v_pulses/x_once_max_a_pulses;
if (x_times > 1)
x_once_max_v_pulses = x_once_max_a_pulses*x_times;
x_len_to_acc = 0;
for (i=0;i<x_times;i++){
x_len_to_acc += (i+1)*x_once_max_a_pulses;
}
y_times = y_once_max_v_pulses/y_once_max_a_pulses;
if (y_times > 1)
y_once_max_v_pulses = y_once_max_a_pulses*y_times;
y_len_to_acc = 0;
for (i=0;i<y_times;i++){
y_len_to_acc += (i+1)*y_once_max_a_pulses;
}
fclose(fp);
return trans_g_to_23();
// printf("If You Want To Send, Press Enter!\n");
// ch = getch();
// if (ch == 13)
// send_file();
}
trans_gfile_to_23_code()
{
FILE *fp;
int i,times,ch;
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;
fp = fopen("c:\\jgs\\setup1.fil","rb");
if (fp == NULL){
return -1;
}
load_mac_para(fp);
xmaxv1 = xmaxv; // Max Speed units(G CODE) per danwei
ymaxv1 = ymaxv;
x_once_max_a_pulses = xmaxa*xsteps*interval/1000.0;
y_once_max_a_pulses = ymaxa*ysteps*interval/1000.0;
x_once_max_a_pulses /= (1000/interval);
y_once_max_a_pulses /= (1000/interval);
x_once_max_v_pulses = xmaxv*xsteps*interval/1000.0;
y_once_max_v_pulses = ymaxv*ysteps*interval/1000.0;
if (danwei == MINUTES){
x_once_max_a_pulses /= 60.0;
y_once_max_a_pulses /= 60.0;
x_once_max_v_pulses /= 60.0;
y_once_max_v_pulses /= 60.0;
}
x_times = x_once_max_v_pulses/x_once_max_a_pulses;
if (x_times > 1)
x_once_max_v_pulses = x_once_max_a_pulses*x_times;
x_len_to_acc = 0;
for (i=0;i<x_times;i++){
x_len_to_acc += (i+1)*x_once_max_a_pulses;
}
y_times = y_once_max_v_pulses/y_once_max_a_pulses;
if (y_times > 1)
y_once_max_v_pulses = y_once_max_a_pulses*y_times;
y_len_to_acc = 0;
for (i=0;i<y_times;i++){
y_len_to_acc += (i+1)*y_once_max_a_pulses;
}
fclose(fp);
return trans_gfile_to_23();
}
/*
get_a_line(FILE *fpe)
{
int i,ch;
ch = fgetc(fpe);
if (ch == EOF)
return -1;
i = 0;
while ((ch != 13)&&(ch != 10)&&(ch != EOF)){
buf[i++] = ch;
ch = fgetc(fpe);
}
buf[i] = 0;
if (ch == EOF)
return -1;
fgetc(fpe);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -