00001
00002
00003
00004
00005
00006
00007
00008 #include <windows.h>
00009 #include <commctrl.h>
00010 #include <fcntl.h>
00011 #include <io.h>
00012 #include <stdio.h>
00013 #include <math.h>
00014 #include <stdlib.h>
00015 #include <string.h>
00016 #include <ctype.h>
00017 #include <sys\types.h>
00018 #include <sys\stat.h>
00019
00020 #include "dxfcon.h"
00021
00022 extern int (*X__pri)(const char *,...);
00023 extern HWND hWndParent;
00024 extern HINSTANCE hInstance;
00025 extern HWND dlg,hwndProgress;
00026 extern BOOL CALLBACK DlgProc(HWND,UINT,WPARAM,LPARAM);
00027 extern BOOL CALLBACK DlgProcScale(HWND,UINT,WPARAM,LPARAM);
00028
00029 #ifndef PI
00030 #define PI 3.1415926
00031 #endif
00032
00033 #ifdef __ZTC__
00034 #ifdef DOS386
00035 #define _far
00036 #endif
00037 #define min(a,b) ( ((a) < (b)) ? (a) : (b) )
00038 #define max(a,b) ( ((a) > (b)) ? (a) : (b) )
00039 #else
00040 #endif
00041
00042 #define HE 2147483647L
00043 #define _X__FAR
00044 #define E_TOOMANYVERTICES 1
00045 #define G_FILEOPENFAIL 2
00046
00047 typedef double vector[3];
00048 typedef long point[3];
00049 typedef long twoLongs[2];
00050
00051 typedef struct VERTEX {
00052 struct VERTEX *last;
00053 struct VERTEX *next;
00054 unsigned short id;
00055 char status,pad;
00056 long xyz[3];
00057 struct SKELETON *sp;
00058 } vertex;
00059
00060 typedef struct EDGE {
00061 struct EDGE *last;
00062 struct EDGE *next;
00063 struct VERTEX *V[2];
00064 } edge;
00065
00066 typedef struct FACE{
00067 struct FACE *last;
00068 struct FACE *next;
00069 struct VERTEX *V[3];
00070 struct VERTEX *extra,*xxtra;
00071 unsigned char color[3],matcol[3],texture,brush;
00072 } face;
00073
00074 typedef struct WIREFRAME {
00075 long Np,Ne;
00076 twoLongs *e;
00077 point *p;
00078 } wireframe;
00079
00080 typedef struct SKELETON {
00081 struct SKELETON *at,*next,*last;
00082 short id,offset;
00083 double t[4][4];
00084 point xyz,tp;
00085 char name[16];
00086 double weight;
00087 } skel;
00088
00089 typedef struct {
00090 vertex Origin;
00091 long Xdirection[3];
00092 long Ydirection[3];
00093 long Zdirection[3];
00094 vertex Offset;
00095 } axis;
00096
00097 typedef struct {
00098 char name[8];
00099 short Nc,Np,flag;
00100 vertex **Vc,**Vp;
00101 } bezier_curve;
00102
00103
00104 #define UNIT 32768L
00105 #define UNIT2 65536L
00106 #define MINUNIT 1024L
00107 #define MAXUNIT 4194304L
00108 #define UNITSCA (MAXUNIT/16)
00109
00110 #define NO 0
00111 #define YES 1
00112 #define OK 0
00113 #define FAIL 1
00114 #define SELECTED 1
00115 #define DESELECTED 0
00116 #define PLANE 0
00117
00118 short quiet=0;
00119 axis ObjectAxis={NULL,NULL,0,0,
00120 0L,0L,0L,
00121 0,0L,0L,
00122 UNIT,0L,0L,
00123 0L,UNIT,0L,
00124 0L,0L,0L};
00125 vertex *MainVp=NULL;
00126 edge *MainEp=NULL;
00127 face *MainFp=NULL;
00128 long Nvert=0,NvertSelect=0,NvertDeselect=0,Nface=0,Nedge=0
00129 ,TVpointX=(-UNIT),TVpointY=(-UNIT),TVpointZ=(-UNIT)
00130 ,TVsizeX=UNIT2,TVsizeY=UNIT2,TVsizeZ=UNIT2;
00131 short ShadowFlag=NO;
00132 point TextureP[64],TextureX[64],TextureY[64];
00133 long TextureType[64],TextureAnimFF[64],TextureAnimDL[64],TextureAnimLF[64],
00134 TextureMap[64],TextureAngle[64];
00135 char *TextureList[64];
00136 long Brilliance=0, Reflectivity=0, Transparency=0;
00137 long EDIT_ACTION,NoTextures=0,Nskel=0,DefaultBrilliance,DefaultTransparency,
00138 DefaultReflectivity,ruler=1,lrulerx=0,lrulery=0,lrulerz=0,grid_size=UNIT,
00139 rulerx=0,rulery=0,rulerz=0,N_Bcurves=0;
00140 skel *MainSp=NULL,*FirstSp=NULL,*TextureSk[64];
00141
00142 void CreateEdge(vertex *v1, vertex *v2, short check);
00143 void CreateFace(vertex *v1, vertex *v2, vertex *v3, short check);
00144 void CreateVertex(void);
00145 void WriteTextures(void);
00146 void WriteVertices(short );
00147 void WriteEdges(short );
00148 void WriteFaces(short );
00149 void WriteSkeleton(void);
00150 static void WriteWireframe(void);
00151 static void WriteBezierCurve(void);
00152
00153 static FILE *fp,*fo;
00154 static long newface,newedge,newvert,minX,minY,minZ,maxX,maxY,maxZ;
00155 static char str[10],ruler_name[8];
00156 static vertex **p,*vlast;
00157 static axis TempAxis;
00158 static face *FaceBase;
00159 static bezier_curve *Bcurves=NULL;
00160 static wireframe w_frame={0,0,NULL,NULL};
00161
00162
00163 void CreateEdge(vertex *v1, vertex *v2, short check)
00164 {edge *temp;
00165 if(check){
00166 temp=MainEp;
00167 while(temp != NULL)
00168 {if( ((temp->V[0] == v1) && (temp->V[1] == v2))
00169 || ((temp->V[0] == v2) && (temp->V[1] == v1)) )return;
00170 temp=temp->last;}}
00171 if(( temp=(edge *)malloc(sizeof(edge)) ) == NULL)return;
00172 if(MainEp != NULL)MainEp->next=temp;
00173 temp->last=MainEp;
00174 temp->V[0]=v1;
00175 temp->V[1]=v2;
00176 temp->next=NULL;
00177 MainEp=temp;
00178 Nedge++;
00179 }
00180
00181 void CreateFace(vertex *v1, vertex *v2, vertex *v3, short check)
00182 {face *temp;
00183 if(check){
00184 temp=MainFp;
00185 while(temp != NULL)
00186 {if( ((temp->V[0] == v1) && (temp->V[1] == v2) && (temp->V[2] == v3))
00187 || ((temp->V[0] == v1) && (temp->V[1] == v3) && (temp->V[2] == v2))
00188 || ((temp->V[0] == v2) && (temp->V[1] == v3) && (temp->V[2] == v1))
00189 || ((temp->V[0] == v2) && (temp->V[1] == v1) && (temp->V[2] == v3))
00190 || ((temp->V[0] == v3) && (temp->V[1] == v1) && (temp->V[2] == v2))
00191 || ((temp->V[0] == v3) && (temp->V[1] == v2) && (temp->V[2] == v1))
00192 )return;
00193 temp=temp->last;}}
00194 if(( temp=(face *)malloc(sizeof(face)) ) == NULL)return;
00195 if(MainFp != NULL)MainFp->next=temp;
00196 temp->last=MainFp;
00197 temp->V[0]=v1;
00198 temp->V[1]=v2;
00199 temp->V[2]=v3;
00200 temp->next=NULL;
00201 temp->color[0]=0;
00202 temp->color[1]=0;
00203 temp->color[2]=0;
00204 temp->matcol[0]=0;
00205 temp->matcol[1]=0;
00206 temp->matcol[2]=0;
00207 temp->texture=0;
00208 temp->brush=0;
00209 temp->extra=NULL;
00210 temp->xxtra=NULL;
00211 MainFp=temp;
00212 Nface++;
00213 }
00214
00215 void CreateVertex(void){
00216 vertex *temp;
00217 if(( temp=(vertex *)malloc(sizeof(vertex)) ) == NULL)return;
00218 if(MainVp != NULL)MainVp->next=temp;
00219 temp->last=MainVp;
00220 temp->xyz[0]=0;
00221 temp->xyz[1]=0;
00222 temp->xyz[2]=0;
00223 temp->next=NULL;
00224 temp->sp=NULL;
00225 temp->status=SELECTED;
00226 temp->id=0;
00227 MainVp=temp;
00228 Nvert++;
00229 NvertSelect++;
00230 }
00231
00232 void CreateSkeleton(skel *at){
00233 skel _X__FAR *temp;
00234 int i,j;
00235 if(( temp=(skel _X__FAR *)malloc(sizeof(skel)) ) == NULL)return;
00236 if(MainSp != NULL)MainSp->next=temp;
00237 temp->last=MainSp;
00238 temp->xyz[0]=0;
00239 temp->xyz[1]=0;
00240 temp->xyz[2]=0;
00241 temp->next=NULL;
00242 temp->at=at;
00243 temp->id=0;
00244 temp->offset=0;
00245 for(i=0;i<4;i++)
00246 for(j=0;j<4;j++){
00247 temp->t[i][j]=0.0;
00248 if(i == j)temp->t[i][j]=1.0;
00249 }
00250 MainSp=temp;
00251 Nskel++;
00252 if(Nskel == 1)strcpy(temp->name,"Root");
00253 else sprintf(temp->name,"%ld",Nskel-1);
00254 }
00255
00256 void check_error(void){
00257 if(ferror(fo) == 0)return;
00258 if(quiet)return;
00259 X__pri("Error Writing model file");
00260 return;
00261 }
00262
00263 void outsht(short sht){
00264 if(fputc((char)(sht >> 8),fo) == EOF)check_error();
00265 if(fputc((char)(sht ),fo) == EOF)check_error();
00266 }
00267
00268 void outlng(long lng){
00269 if(fputc((char)(lng >> 24),fo) == EOF)check_error();
00270 if(fputc((char)(lng >> 16),fo) == EOF)check_error();
00271 if(fputc((char)(lng >> 8),fo) == EOF)check_error();
00272 if(fputc((char)(lng ),fo) == EOF)check_error();
00273 }
00274
00275 short SaveObject(char *Filename, short status){
00276 long pos1,pos2,pos3,pos4,pos5,pos6,pos7,pos8,pos9,posA,pose;
00277 long FORMsize;
00278 short id,short_form=0;
00279 FORMsize=0; newvert=0; newface=0; newedge=0;
00280 if( (fo=fopen(Filename,"wb")) == NULL){
00281 return 1;
00282 }
00283 fprintf(fo,"FORM");
00284 pos1=ftell(fo); outlng(FORMsize);
00285 fprintf(fo,"OFXM");
00286 fprintf(fo,"AXIS"); outlng(48L);
00287 if(status == 2){
00288 outlng(0); outlng(0); outlng(0);
00289 }
00290 else{
00291 outlng(ObjectAxis.Origin.xyz[0]);
00292 outlng(ObjectAxis.Origin.xyz[1]);
00293 outlng(ObjectAxis.Origin.xyz[2]);
00294 }
00295 outlng(TextureP[63][0]);
00296 outlng(TextureP[63][1]);
00297 outlng(TextureP[63][2]);
00298 outlng(TextureX[63][0]);
00299 outlng(TextureX[63][1]);
00300 outlng(TextureX[63][2]);
00301 outlng(TextureY[63][0]);
00302 outlng(TextureY[63][1]);
00303 outlng(TextureY[63][2]);
00304 fprintf(fo,"OFFS"); outlng(16L);
00305 outlng(3L);
00306 outlng(ObjectAxis.Offset.xyz[0]);
00307 outlng(ObjectAxis.Offset.xyz[1]);
00308 outlng(ObjectAxis.Offset.xyz[2]);
00309 if(Nvert > 0){
00310 fprintf(fo,"VERT"); pos2=ftell(fo); outlng(12*newvert);
00311 WriteVertices(status);
00312 }
00313 else pos2 = -1;
00314 if(Nedge > 0){
00315 fprintf(fo,"EDGE"); pos3=ftell(fo); outlng( 8*newedge); WriteEdges(0);
00316 }
00317 else pos3 = -1;
00318 if(Nface > 0){
00319 fprintf(fo,"FACE"); pos4=ftell(fo); outlng(20*newface); WriteFaces(0);
00320 }
00321 else pos4 = -1;
00322 if(NoTextures > 0){
00323 fprintf(fo,"TEXT"); pos5=ftell(fo); outlng(0L);
00324 WriteTextures();
00325 pos6=ftell(fo);
00326 }
00327 else pos5 = -1;
00328 if((status == 1 || status == 3) &&
00329 Nskel > 1){
00330 fprintf(fo,"SK01"); pos7=ftell(fo); outlng(0L);
00331 WriteSkeleton();
00332 pos8=ftell(fo);
00333 }
00334 else pos7 = -1;
00335 fprintf(fo,"MATL"); outlng((long)(5*sizeof(long)));
00336 outlng(4L);
00337 outlng((long)ShadowFlag);
00338 outlng(DefaultBrilliance);
00339 outlng(DefaultTransparency);
00340 outlng(DefaultReflectivity);
00341 fprintf(fo,"UNIT"); outlng((long)(7*sizeof(long)+8));
00342 outlng((long)ruler);
00343 strcpy(ruler_name,"none");
00344 fwrite(ruler_name,1,8,fo);
00345 outlng(lrulerx); outlng(lrulery); outlng(lrulerz);
00346 outlng(rulerx); outlng(rulery); outlng(rulerz);
00347 fprintf(fo,"GRID"); outlng((long)(2*sizeof(long)));
00348 outlng(grid_size); outlng(grid_size);
00349 WriteWireframe();
00350 if((status == 1 || status == 3) && N_Bcurves > 0){
00351 fprintf(fo,"BEZ1"); pos9=ftell(fo); outlng(0L);
00352 WriteBezierCurve();
00353 posA=ftell(fo);
00354 }
00355 else pos9 = -1;
00356 pose=ftell(fo);
00357 if(pos2 > 0){fseek(fo,pos2,0); outlng(12*newvert);}
00358 if(short_form){
00359 if(pos3 > 0){fseek(fo,pos3,0); outlng( 4*newedge);}
00360 if(pos4 > 0){fseek(fo,pos4,0); outlng(14*newface);}
00361 }
00362 else{
00363 if(pos3 > 0){fseek(fo,pos3,0); outlng( 8*newedge);}
00364 if(pos4 > 0){fseek(fo,pos4,0); outlng(20*newface);}
00365 }
00366 if(pos5 > 0){fseek(fo,pos5,0); outlng(pos6-pos5-4);}
00367 if(pos7 > 0){fseek(fo,pos7,0); outlng(pos8-pos7-4);}
00368 if(pos9 > 0){fseek(fo,pos9,0); outlng(posA-pos9-4);}
00369 fseek(fo,pos1,0); outlng(pose-8);
00370 fclose(fo);
00371 return 0;
00372 }
00373
00374 void WriteTextures(void){
00375 long i,temp;
00376 skel _X__FAR *sp;
00377 i=0;
00378 sp=FirstSp; while(sp != NULL){
00379 sp->id=i; i++;
00380 sp=sp->next;
00381 }
00382 fwrite(&NoTextures,1,1,fo);
00383 for(i=0;i<NoTextures;i++){
00384 outlng(256);
00385 fwrite(TextureList[i],1,256,fo);
00386 outlng(TextureP[i][0]); outlng(TextureP[i][1]); outlng(TextureP[i][2]);
00387 outlng(TextureX[i][0]); outlng(TextureX[i][1]); outlng(TextureX[i][2]);
00388 outlng(TextureY[i][0]); outlng(TextureY[i][1]); outlng(TextureY[i][2]);
00389 temp=TextureType[i]; outlng(temp);
00390 temp=TextureAnimFF[i]; outlng(temp);
00391 temp=TextureAnimDL[i]; outlng(temp);
00392 temp=TextureAnimLF[i]; outlng(temp);
00393 if(TextureSk[i] != NULL){
00394 temp=((TextureSk[i])->id)+1; outlng(temp);
00395 }
00396 else{
00397 temp=0; outlng(temp);
00398 }
00399 temp=TextureMap[i]; outlng(temp);
00400 temp=1; outlng(temp);
00401 temp=TextureAngle[i]; outlng(temp);
00402 }
00403 }
00404
00405 void WriteVertices(short status){
00406 long i;
00407 vertex _X__FAR *vp,*fv;
00408 fv=NULL; vp=MainVp; while(vp != NULL){fv=vp; vp=vp->last;}
00409 i=0;
00410 vp=fv; while(vp != NULL){
00411 if(vp->status == SELECTED){
00412 outlng(vp->xyz[0]); outlng(vp->xyz[1]); outlng(vp->xyz[2]);
00413 vp->pad = (char)(i >> 16);
00414 vp->id=(unsigned short)(i & 0x0000ffff);
00415 i++; newvert++;
00416 }
00417 else vp->pad = -1;
00418 vp=vp->next;
00419 }
00420 }
00421
00422 void WriteEdges(short short_form){
00423 long id;
00424 edge _X__FAR *ep;
00425 ep=MainEp;
00426 while(ep != NULL){
00427 if((ep->V[0]->pad >= 0) && (ep->V[1]->pad >= 0)){
00428 id=(((long)ep->V[0]->id) | ((long)ep->V[0]->pad << 16)); outlng(id);
00429 id=(((long)ep->V[1]->id) | ((long)ep->V[1]->pad << 16)); outlng(id);
00430 newedge++;
00431 }
00432 ep=ep->last;
00433 }
00434 }
00435
00436 void WriteFaces(short short_form){
00437 face _X__FAR *f,*ff;
00438 long id;
00439 ff=NULL; f=MainFp; while(f != NULL){ff=f; f=f->last;}
00440 if((f=ff) != NULL)while(f != NULL){
00441 if((f->V[0]->pad >= 0) &&
00442 (f->V[1]->pad >= 0) &&
00443 (f->V[2]->pad >= 0)){
00444 id=(((long)f->V[0]->id) | ((long)f->V[0]->pad << 16)); outlng(id);
00445 id=(((long)f->V[1]->id) | ((long)f->V[1]->pad << 16)); outlng(id);
00446 id=(((long)f->V[2]->id) | ((long)f->V[2]->pad << 16)); outlng(id);
00447 fwrite(f->color,1,3,fo);
00448 fwrite(&f->texture,1,1,fo);
00449 fwrite(f->matcol,1,3,fo);
00450 fwrite(&f->brush,1,1,fo);
00451 newface++;
00452 }
00453 f=f->next;
00454 }
00455 }
00456
00457 static void WriteBezierCurve(void){
00458 long i,j,id;
00459 outlng(N_Bcurves);
00460 for(i=0;i<N_Bcurves;i++){
00461 outlng((long)Bcurves[i].Nc); outlng((long)Bcurves[i].Np);
00462 fwrite((char *)Bcurves[i].name,1,8,fo);
00463 for(j=0;j<Bcurves[i].Nc;j++){
00464 id=( (long)((Bcurves[i].Vc[j])->id) |
00465 ((long)((Bcurves[i].Vc[j])->pad) << 16));
00466 outlng(id);
00467 }
00468 for(j=0;j<Bcurves[i].Np;j++){
00469 id=((long)((Bcurves[i].Vp[j])->id) |
00470 ((long)((Bcurves[i].Vp[j])->pad) << 16));
00471 outlng(id);
00472 }
00473 }
00474 }
00475
00476 void WriteSkeleton(void){
00477 short i;
00478 skel _X__FAR *sp;
00479 vertex _X__FAR *vp, *fv;
00480 i=0;
00481 sp=FirstSp; while(sp != NULL){
00482 sp->id=i; i++;
00483 sp=sp->next;
00484 }
00485 outlng(Nskel);
00486 sp=FirstSp; while(sp != NULL){
00487 outlng(sp->xyz[0]); outlng(sp->xyz[1]); outlng(sp->xyz[2]);
00488 if(sp->at != NULL)outsht(sp->at->id);
00489 else outsht(0);
00490 fwrite(sp->name,sizeof(char),16,fo);
00491 sp=sp->next;
00492 }
00493 outlng(Nvert);
00494 fv=NULL; vp=MainVp; while(vp != NULL){fv=vp; vp=vp->last;}
00495 vp=fv; while(vp != NULL){
00496 if(vp->sp != NULL)outsht(vp->sp->id);
00497 else outsht(0);
00498 vp=vp->next;
00499 }
00500 }
00501
00502 static void WriteWireframe(void){
00503 long i;
00504 if(w_frame.Np == 0 || w_frame.Ne == 0)return;
00505 fprintf(fo,"WFRM");
00506 outlng((long)(((w_frame.Np*3)+(w_frame.Ne*2)+2)*sizeof(long)));
00507 outlng(w_frame.Np); outlng(w_frame.Ne);
00508 for(i=0;i<w_frame.Np;i++){
00509 outlng(w_frame.p[i][0]);
00510 outlng(w_frame.p[i][1]);
00511 outlng(w_frame.p[i][2]);
00512 }
00513 for(i=0;i<w_frame.Ne;i++){
00514 outlng(w_frame.e[i][0]); outlng(w_frame.e[i][1]);
00515 }
00516 }
00517
00518 #include "dxfcon2.c"
00519
00520 BOOL DoDxfConvert(char *infile, char *outfile, double wt,
00521 short h_type,short l_freeze, short b_freeze, short no_weld,
00522 short d2, short d3, short cs){
00523 long i;
00524 short error,report=0;
00525 char argv1[256];
00526 CreateSkeleton(NULL);
00527 FirstSp=MainSp;
00528 ruler=32768;
00529 TextureP[63][0]=0; TextureP[63][1]=0; TextureP[63][2]=TVpointZ;
00530 TextureX[63][0]=TVsizeX/2; TextureX[63][1]=0;
00531 TextureX[63][2]=TVpointZ;
00532 TextureY[63][0]=0; TextureY[63][1]=TVsizeZ/2; TextureY[63][2]=TVpointZ;
00533 TextureSk[63]=NULL;
00534 TextureMap[63]=PLANE;
00535 TextureType[63]=0;
00536 if(quiet)report=0;
00537 strcpy(argv1,infile);
00538 if((i=strlen(argv1)) < 4 || argv1[i-4] != '.')strcat(argv1,".DXF");
00539 error=DxfLoad(argv1,h_type,l_freeze,b_freeze,report,wt,no_weld,d2,d3,cs);
00540 if(error){
00541 if(quiet){
00542 if(error == 5)return FALSE;
00543 return FALSE;
00544 }
00545 if(error == 5)X__pri("Model is too BIG try increasing the weld threshold");
00546 if(error == 2)X__pri("DXF file truncated");
00547 if(error == 1)X__pri("Failed to open file");
00548 return FALSE;
00549 }
00550 strcpy(argv1,outfile);
00551 if((i=strlen(argv1)) < 4 || argv1[i-4] != '.')strcat(argv1,".mfx");
00552 error=SaveObject(argv1,1);
00553 if(quiet){
00554 if(error)return FALSE;
00555 return TRUE;
00556 }
00557 if(error)X__pri("Error Opening model output file");
00558 else X__pri("Build finished: [ %ld Vertices %ld Faces ]",Nvert,Nface);
00559 return TRUE;
00560 }