📄 converter.c
字号:
#define PI 3.14159265359
#include <windows.h>
#include <process.h>
#include <alloc.h>
#include <stdio.h>
#include <math.h>
#include "converter.h"
#include "ace.h"
extern struct layer_obj *layer ;
extern struct priority_obj *priority ;
extern struct convert_obj convertop ;
extern int demo;
extern HWND hWindow;
extern int iLayerCount, iPrioCount;
extern char szDxfFile[_MAX_PATH], szToFileName[_MAX_PATH];
int get_values(FILE *, struct entity_obj *);
int get_string(FILE *, const char *, char *);
void get_first_point(double *, double *, double *, struct entity_obj *);
void get_second_point(double *, double *, double *, struct entity_obj *);
void make_arc(struct entity_obj *);
void WINAPI convert(void)
{
int ctemp, zchar, wchar, achar, cchar, count=0, closed, msg=0;
long i, lay_index, k, j, l, temp, num_of_ent, line_num=1, cur_ent, cur_dir, delay, display;
double x1, y1, z1, ztemp, x2, y2, z2, dist1, dist2, cur_dist, zref, x1st=0, y1st=0, z1st=0;
static char string[100], type[100], status[100];
static struct entity_obj temp_ent, temp_ent2;
static far struct entity_obj *entity;
FILE *ifp, *ofp;
fpos_t pos;
sprintf(status,"Begin Converting Process");
SetWindowText(GetDlgItem(hWindow, IDD_STATUS),status);
if((ifp=fopen(szDxfFile,"r"))==NULL)
{sprintf(status,"Invalid .dxf File Format");
SetWindowText(GetDlgItem(hWindow, IDD_STATUS),status);
return;
}
if((ofp=fopen(szToFileName,"w"))==NULL)
{fclose(ifp);
sprintf(status,"Invalid Output File");
SetWindowText(GetDlgItem(hWindow, IDD_STATUS),status);
return;
}
if((entity=(struct entity_obj *)farcalloc(1,sizeof(struct entity_obj)))==NULL) {msg=2; goto DONE;}
for(i=0;i<iPrioCount;i++)
{//alocate and store entities
num_of_ent=0; display=100;
while(1)
{if((temp=fscanf(ifp,"%s",string))==0 || temp==EOF) break;
if(strcmp(string,"0")!=0) continue;
fgetpos(ifp,&pos);
if((temp=fscanf(ifp,"%s",type))==0 || temp==EOF) break;
if(num_of_ent>=display)
{display=num_of_ent+100;
sprintf(status,"Stored %u Ent., Prio. %u",num_of_ent,i+1);
SetWindowText(GetDlgItem(hWindow, IDD_STATUS),status);
}
if(strcmp(type,"LINE")==0 || strcmp(type,"CIRCLE")==0 ||
strcmp(type,"ARC")==0 || strcmp(type,"POINT")==0 || strcmp(type,"POLYLINE")==0)
{if(get_string(ifp,"8",string)==0) {msg=1; goto DONE;}
for(lay_index=0;lay_index<iLayerCount;lay_index++)
if(strcmp(layer[lay_index].name,string)==0 &&
layer[lay_index].priority-1==i &&
layer[lay_index].status==TRUE) break;
if(lay_index==iLayerCount) continue;
if(strcmp(type,"POLYLINE")!=0)
{if((entity=farrealloc(entity,(++num_of_ent)*sizeof(struct entity_obj)))==NULL) {msg=2; goto DONE;}
}
}
else if(strcmp(type,"BLOCK")==0)
{while(1)
{if((temp=fscanf(ifp,"%s",string))==0 || temp==EOF) {msg=1; goto DONE;}
if(strcmp(string,"0")==0)
{fgetpos(ifp, &pos);
if((temp=fscanf(ifp,"%s",string))==0 || temp==EOF) {msg=1; goto DONE;}
if(strcmp(string,"ENDBLK")==0) break;
else fsetpos(ifp, &pos);
}
}
continue;
}
else
{fsetpos(ifp,&pos);
continue;
}
if(strcmp(type,"POLYLINE")==0)
{delay=zref=0;
if((temp=get_values(ifp,&temp_ent2))==0) {msg=1; goto DONE;}
if(temp==2) closed=TRUE;
else closed=FALSE;
zref=temp_ent2.z1;
while((temp=fscanf(ifp,"%s",string))!=EOF && temp!=0)
{if(strcmp(string,"SEQEND")==0 && closed==FALSE) break;
if(strcmp(string,"VERTEX")==0 || (strcmp(string,"SEQEND")==0 && closed==TRUE))
{if(strcmp(string,"SEQEND")==0 && closed==TRUE)
{temp_ent.x2=x1st; temp_ent.y2=y1st; temp_ent.z2=z1st;
}
else
{if(get_values(ifp,&temp_ent2)==0) {msg=1; goto DONE;}
temp_ent.x2=temp_ent2.x1;
temp_ent.y2=temp_ent2.y1;
temp_ent.z2=temp_ent2.z1+zref;
}
if(delay>0)
{if((entity=farrealloc(entity,(++num_of_ent)*sizeof(struct entity_obj)))==NULL) {msg=2; goto DONE;}
entity[num_of_ent-1]=temp_ent;
if(entity[num_of_ent-1].type==ARC) make_arc(&entity[num_of_ent-1]);
entity[num_of_ent-1].layer=lay_index;
if(entity[num_of_ent-1].type==LINE)
{entity[num_of_ent-1].dir=FOR;
entity[num_of_ent-1].z1=entity[num_of_ent-1].z1+layer[lay_index].zoffset;
entity[num_of_ent-1].z2=entity[num_of_ent-1].z2+layer[lay_index].zoffset;
for(k=1;k*-layer[lay_index].depth>min(entity[num_of_ent-1].z1,entity[num_of_ent-1].z2);k++)
{if((entity=farrealloc(entity,(++num_of_ent)*sizeof(struct entity_obj)))==NULL) {msg=2; goto DONE;}
entity[num_of_ent-1]=entity[num_of_ent-2];
if(entity[num_of_ent-2].z1<k*-layer[lay_index].depth) entity[num_of_ent-2].z1=k*-layer[lay_index].depth;
if(entity[num_of_ent-2].z2<k*-layer[lay_index].depth) entity[num_of_ent-2].z2=k*-layer[lay_index].depth;
}
}
if(entity[num_of_ent-1].type==ARC)
{entity[num_of_ent-1].z1=entity[num_of_ent-1].z1+layer[lay_index].zoffset;
if(layer[lay_index].arc==IDD_CCWARC || layer[lay_index].arc==IDD_EITHERARC) entity[num_of_ent-1].dir=CCW;
else entity[num_of_ent-1].dir=CW;
for(k=1;k*-layer[lay_index].depth>entity[num_of_ent-1].z1;k++)
{if((entity=farrealloc(entity,(++num_of_ent)*sizeof(struct entity_obj)))==NULL) {msg=2; goto DONE;}
entity[num_of_ent-1]=entity[num_of_ent-2];
if(entity[num_of_ent-2].z1<k*-layer[lay_index].depth) entity[num_of_ent-2].z1=k*-layer[lay_index].depth;
}
}
}
else
{delay++;
x1st=temp_ent.x2; y1st=temp_ent.y2; z1st=temp_ent.z2;
}
if(strcmp(string,"SEQEND")==0 && closed==TRUE) break;
temp_ent.radius=temp_ent2.radius;
if(temp_ent.radius!=0) temp_ent.type=ARC;
else temp_ent.type=LINE;
temp_ent.x1=temp_ent.x2;
temp_ent.y1=temp_ent.y2;
temp_ent.z1=temp_ent.z2;
}
}
if(temp==EOF || temp==0) break;
continue;
}
if(strcmp(type,"LINE")==0 || strcmp(type,"POINT")==0)
{if(strcmp(type,"LINE")==0) entity[num_of_ent-1].type=LINE;
else entity[num_of_ent-1].type=POINT;
entity[num_of_ent-1].layer=lay_index;
if(strcmp(type,"LINE")==0) entity[num_of_ent-1].dir=FOR;
if(get_values(ifp,&entity[num_of_ent-1])==0) {msg=1; goto DONE;}
entity[num_of_ent-1].z1=entity[num_of_ent-1].z1+layer[lay_index].zoffset;
if(strcmp(type,"LINE")==0) entity[num_of_ent-1].z2=entity[num_of_ent-1].z2+layer[lay_index].zoffset;
for(k=1;k*-layer[lay_index].depth>min(entity[num_of_ent-1].z1,entity[num_of_ent-1].z2);k++)
{if((entity=farrealloc(entity,(++num_of_ent)*sizeof(struct entity_obj)))==NULL) {msg=2; goto DONE;}
entity[num_of_ent-1]=entity[num_of_ent-2];
if(entity[num_of_ent-2].z1<k*-layer[lay_index].depth) entity[num_of_ent-2].z1=k*-layer[lay_index].depth;
if(entity[num_of_ent-2].z2<k*-layer[lay_index].depth) entity[num_of_ent-2].z2=k*-layer[lay_index].depth;
}
continue;
}
if(strcmp(type,"ARC")==0 || strcmp(type,"CIRCLE")==0)
{entity[num_of_ent-1].type=ARC;
entity[num_of_ent-1].layer=lay_index;
if(layer[lay_index].arc==IDD_CCWARC || layer[lay_index].arc==IDD_EITHERARC) entity[num_of_ent-1].dir=CCW;
else entity[num_of_ent-1].dir=CW;
if(get_values(ifp,&entity[num_of_ent-1])==0) {msg=1; goto DONE;}
entity[num_of_ent-1].z1=entity[num_of_ent-1].z1+layer[lay_index].zoffset;
if(strcmp(type,"ARC")==0)
{entity[num_of_ent-1].ang_start=entity[num_of_ent-1].ang_start*2*PI/360;
entity[num_of_ent-1].ang_end=entity[num_of_ent-1].ang_end*2*PI/360;
}
else
{entity[num_of_ent-1].ang_start=0;
entity[num_of_ent-1].ang_end=2*PI;
}
for(k=1;k*-layer[lay_index].depth>entity[num_of_ent-1].z1;k++)
{if((entity=farrealloc(entity,(++num_of_ent)*sizeof(struct entity_obj)))==NULL) {msg=2; goto DONE;}
entity[num_of_ent-1]=entity[num_of_ent-2];
if(entity[num_of_ent-2].z1<k*-layer[lay_index].depth) entity[num_of_ent-2].z1=k*-layer[lay_index].depth;
}
continue;
}
}
rewind(ifp);
//optimize if necessary
if(priority[i].optimize==TRUE)
{x1=y1=0; z1=priority[i].release; display=100;
for(j=0;j<num_of_ent;j++)
{if(j+1==display)
{display=display+100;
sprintf(status,"Optimizing Prio. %u, %d%% Complete",i+1, (int)(j+1)*100/num_of_ent);
SetWindowText(GetDlgItem(hWindow, IDD_STATUS),status);
}
for(k=j;k<num_of_ent;k++)
{if(entity[k].type==ARC && layer[entity[k].layer].arc==IDD_CCWARC)
{get_first_point(&x2, &y2, &z2, &entity[k]);
dist1=sqrt(pow(x2-x1,2)+pow(y2-y1,2)+pow(z2-z1,2));
if(j==k) cur_dist=dist1;
if(dist1<cur_dist) {cur_ent=k; cur_dir=CCW; cur_dist=dist1;}
}
else if(entity[k].type==ARC && layer[entity[k].layer].arc==IDD_CWARC)
{get_second_point(&x2, &y2, &z2, &entity[k]);
dist1=sqrt(pow(x2-x1,2)+pow(y2-y1,2)+pow(z2-z1,2));
if(j==k) cur_dist=dist1;
if(dist1<=cur_dist) {cur_ent=k; cur_dir=CW; cur_dist=dist1;}
}
else if(entity[k].type==ARC)
{get_first_point(&x2, &y2, &z2, &entity[k]);
dist1=sqrt(pow(x2-x1,2)+pow(y2-y1,2)+pow(z2-z1,2));
get_second_point(&x2, &y2, &z2, &entity[k]);
dist2=sqrt(pow(x2-x1,2)+pow(y2-y1,2)+pow(z2-z1,2));
if(j==k) cur_dist=min(dist1,dist2);
if(dist1<=cur_dist) {cur_ent=k; cur_dir=CCW; cur_dist=dist1;}
if(dist2<=cur_dist) {cur_ent=k; cur_dir=CW; cur_dist=dist2;}
}
else if(entity[k].type==LINE)
{get_first_point(&x2, &y2, &z2, &entity[k]);
dist1=sqrt(pow(x2-x1,2)+pow(y2-y1,2)+pow(z2-z1,2));
get_second_point(&x2, &y2, &z2, &entity[k]);
dist2=sqrt(pow(x2-x1,2)+pow(y2-y1,2)+pow(z2-z1,2));
if(j==k) cur_dist=min(dist1, dist2);
if(dist1<=cur_dist)
{cur_ent=k; cur_dir=FOR; cur_dist=dist1;}
if(dist2<=cur_dist) {cur_ent=k; cur_dir=REV; cur_dist=dist2;}
}
else if(entity[k].type==POINT)
{get_first_point(&x2, &y2, &z2, &entity[k]);
dist1=sqrt(pow(x2-x1,2)+pow(y2-y1,2)+pow(z2-z1,2));
if(j==k) cur_dist=dist1;
if(dist1<=cur_dist) {cur_ent=k; cur_dir=REV; cur_dist=dist1;}
}
}
entity[cur_ent].dir=cur_dir;
temp_ent=entity[j]; entity[j]=entity[cur_ent]; entity[cur_ent]=temp_ent;
get_second_point(&x1, &y1, &ztemp, &entity[j]);
}
}
//write precode to file
if(demo==FALSE || (demo==TRUE && count<=20))
{fprintf(ofp,"%s",priority[i].precode);
if(strlen(priority[i].precode)>0 &&
priority[i].precode[strlen(priority[i].precode)-1]!='\n') fputc('\n',ofp);
}
//write code to file
display=100;
for(k=0;k<num_of_ent;k++)
{//entity overhead
if(k+1==display)
{display=display+100;
sprintf(status,"Writing Prio. %u, %d%% Complete",i+1, (int)(k+1)*100/num_of_ent);
SetWindowText(GetDlgItem(hWindow, IDD_STATUS),status);
}
if(demo==TRUE) {if(count<=20) count++; if(count>20) break;}
get_first_point(&x2, &y2, &z2, &entity[k]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -