home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / utility / dos / dxfraw / dxf2raw2.c next >
Encoding:
C/C++ Source or Header  |  1992-07-30  |  4.0 KB  |  148 lines

  1.  
  2. /*
  3.  
  4.     dxf2v -- strip 3d faces out of a .dfx file
  5.  
  6.     Copyright 1991 Stephen B. Coy
  7.      modified by   Tom Barber    april 3 1992  added path recognition
  8.                            added layer extraction
  9.  
  10.      modified by   Mike Daigle   july 3 1992   small fix to layer
  11.                              extraction
  12.                            added surface color
  13.                              extraction
  14.      modified by   Adam Shiffman july 30 1992  removed surface color
  15.                            code, added RAW output
  16. */
  17.  
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <math.h>
  22. #include <string.h>
  23. main(int argc, char *argv[])
  24. {
  25.     char    str[80];
  26.     int     key, npnts,layer,l;
  27.     double  x1, x2, x3, x4,
  28.         y1, y2, y3, y4,
  29.         z1, z2, z3, z4;
  30.     FILE    *dxf_in_file,*raw_out_file;
  31.  
  32.  
  33.     if(argc < 3) {
  34.         printf("\nDXF2RAW2 Version 1.0 - Adapted from Stephen Coy's DXF2V\n");
  35.         printf("Converts 3DFACEs in DXF files to RAW files of X,Y,Z points.\n");
  36.         printf("Extracts by Layers (if desired)\n");
  37.         printf("Added code by Tom Barber 4/92  Mike Daigle 7/92\n");
  38.         printf("Modified code to output RAW triangles 7/92 Adam Shiffman\n\n");
  39.     printf("Usage: dxf2raw2 file.dxf file.raw [Layer Number]\n\n");
  40.         printf("Where:\n");
  41.         printf("       file.dxf = name of dxf file with extension\n");
  42.         printf("       file.raw = name of RAW file with extension\n");
  43.         printf("       [Layer #]= Layer to be converted.  Must be specified,\n");
  44.         printf(" Typical: DXF2RAW2 art.dxf art.raw -1\n");
  45.         printf("       convert art.dxf to art.raw all layers\n");
  46.         exit(1);
  47.     }
  48.  
  49.     //Open the files
  50.     if (!(dxf_in_file = fopen(argv[1],"rb")))
  51.         { puts("Can not open DXF file!");
  52.           exit(1);
  53.         }
  54.  
  55.     if (!(raw_out_file = fopen(argv[2],"w")))
  56.         { puts("Can not open RAW file for output!");
  57.           fclose(dxf_in_file);
  58.           exit(1);
  59.         }
  60.  
  61. layer = -1;
  62.  
  63. if (argc > 3) layer = atoi(argv[3]);
  64.  
  65.     while(fgets(str, 80, dxf_in_file)) {
  66.         //Wait until we find a 3DFACE...
  67.         if(strncmp(str, "3DFACE", 6) != 0) {continue;}
  68.            fgets(str,80,dxf_in_file);
  69.            if (atoi(str) != 8) {printf("WARNING...LAYER...MISSED\n");}
  70.            fgets(str,80,dxf_in_file);
  71.  
  72.            if (layer != atoi(str) && layer != -1)   {continue;}
  73.  
  74.         x1 = x2 = x3 = x4 = y1 = y2 = y3 = y4 = z1 = z2 = z3 = z4 = 0.0;
  75.         do {
  76.             fgets(str, 80, dxf_in_file);
  77.             key = atoi(str);
  78.             fgets(str, 80, dxf_in_file);
  79.             switch(key) {
  80.                 case 10 : x1 = atof(str); break;
  81.                 case 20 : y1 = atof(str); break;
  82.                 case 30 : z1 = atof(str); break;
  83.                 case 11 : x2 = atof(str); break;
  84.                 case 21 : y2 = atof(str); break;
  85.                 case 31 : z2 = atof(str); break;
  86.                 case 12 : x3 = atof(str); break;
  87.                 case 22 : y3 = atof(str); break;
  88.                 case 32 : z3 = atof(str); break;
  89.                 case 13 : x4 = atof(str); break;
  90.                 case 23 : y4 = atof(str); break;
  91.                 case 33 : z4 = atof(str); break;
  92.                 default : break;
  93.             }
  94.         } while(key != 33);
  95.  
  96.         /* check for non-unique points */
  97.  
  98.         npnts = 4;
  99.         if(x1==x2 && y1==y2 && z1==z2) {
  100.             x2 = x3; x3 = x4;
  101.             y2 = y3; y3 = y4;
  102.             z2 = z3; z3 = z4;
  103.             npnts--;
  104.         }
  105.         if(npnts < 3) continue;
  106.         if(x1==x2 && y1==y2 && z1==z2) {
  107.             continue;
  108.         }
  109.         if(x1==x3 && y1==y3 && z1==z3) {
  110.             x3 = x4;
  111.             y3 = y4;
  112.             z3 = z4;
  113.             npnts--;
  114.         }
  115.         if(npnts < 3) continue;
  116.         if(x2==x3 && y2==y3 && z2==z3) {
  117.             x3 = x4;
  118.             y3 = y4;
  119.             z3 = z4;
  120.             npnts--;
  121.         }
  122.         if(npnts < 3) continue;
  123.         if(npnts==4 && x4==x3 && y4==y3 && z4==z3) {
  124.             npnts--;
  125.         }
  126.         if(npnts < 3) continue;
  127.  
  128.         /* got the poly, output it */
  129.  
  130.         if(npnts == 4) {
  131.         fprintf(raw_out_file,"%.4f %.4f %.4f\n", x1, y1, z1);
  132.         fprintf(raw_out_file,"%.4f %.4f %.4f\n", x2, y2, z2);
  133.         fprintf(raw_out_file,"%.4f %.4f %.4f\n}\n", x3, y3, z3);
  134.         fprintf(raw_out_file,"%.4f %.4f %.4f\n", x1, y1, z1);
  135.         fprintf(raw_out_file,"%.4f %.4f %.4f\n", x3, y3, z3);
  136.         fprintf(raw_out_file,"%.4f %.4f %.4f\n}\n", x4, y4, z4);
  137.         } else {
  138.         fprintf(raw_out_file,"%.4f %.4f %.4f\n", x1, y1, z1);
  139.         fprintf(raw_out_file,"%.4f %.4f %.4f\n", x2, y2, z2);
  140.         fprintf(raw_out_file,"%.4f %.4f %.4f\n}\n", x3, y3, z3);
  141.     }   /* END WHILE */
  142.  
  143.    }
  144.  
  145. fclose(raw_out_file);
  146. fclose(dxf_in_file);
  147. }       /* end of main() */
  148.