home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / utilities / utilsd / dxf2draw / c / DXF2RAW2
Encoding:
Text File  |  1994-10-07  |  4.2 KB  |  151 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.          Archimedes version ported by Alex McLintock. alexmc@biccdc.co.uk
  18.          All I had to do was change the "//" style comments! I like portable code!
  19. */
  20.  
  21.  
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <math.h>
  25. #include <string.h>
  26. main(int argc, char *argv[])
  27. {
  28.     char    str[80];
  29.     int     key, npnts,layer,l;
  30.     double  x1, x2, x3, x4,
  31.         y1, y2, y3, y4,
  32.         z1, z2, z3, z4;
  33.     FILE    *dxf_in_file,*raw_out_file;
  34.  
  35.  
  36.     if(argc < 3) {
  37.         printf("\nDXF2RAW2 Version 1.0 - Adapted from Stephen Coy's DXF2V\n");
  38.         printf("Converts 3DFACEs in DXF files to RAW files of X,Y,Z points.\n");
  39.         printf("Extracts by Layers (if desired)\n");
  40.         printf("Added code by Tom Barber 4/92  Mike Daigle 7/92\n");
  41.         printf("Modified code to output RAW triangles 7/92 Adam Shiffman\n\n");
  42.     printf("Usage: dxf2raw2 file.dxf file.raw [Layer Number]\n\n");
  43.         printf("Where:\n");
  44.         printf("       file.dxf = name of dxf file with extension\n");
  45.         printf("       file.raw = name of RAW file with extension\n");
  46.         printf("       [Layer #]= Layer to be converted.  Must be specified,\n");
  47.         printf(" Typical: DXF2RAW2 art.dxf art.raw -1\n");
  48.         printf("       convert art.dxf to art.raw all layers\n");
  49.         exit(1);
  50.     }
  51.  
  52.     /*Open the files*/
  53.     if (!(dxf_in_file = fopen(argv[1],"rb")))
  54.         { puts("Can not open DXF file!");
  55.           exit(1);
  56.         }
  57.  
  58.     if (!(raw_out_file = fopen(argv[2],"w")))
  59.         { puts("Can not open RAW file for output!");
  60.           fclose(dxf_in_file);
  61.           exit(1);
  62.         }
  63.  
  64. layer = -1;
  65.  
  66. if (argc > 3) layer = atoi(argv[3]);
  67.  
  68.     while(fgets(str, 80, dxf_in_file)) {
  69.         /*Wait until we find a 3DFACE...*/
  70.         if(strncmp(str, "3DFACE", 6) != 0) {continue;}
  71.            fgets(str,80,dxf_in_file);
  72.            if (atoi(str) != 8) {printf("WARNING...LAYER...MISSED\n");}
  73.            fgets(str,80,dxf_in_file);
  74.  
  75.            if (layer != atoi(str) && layer != -1)   {continue;}
  76.  
  77.         x1 = x2 = x3 = x4 = y1 = y2 = y3 = y4 = z1 = z2 = z3 = z4 = 0.0;
  78.         do {
  79.             fgets(str, 80, dxf_in_file);
  80.             key = atoi(str);
  81.             fgets(str, 80, dxf_in_file);
  82.             switch(key) {
  83.                 case 10 : x1 = atof(str); break;
  84.                 case 20 : y1 = atof(str); break;
  85.                 case 30 : z1 = atof(str); break;
  86.                 case 11 : x2 = atof(str); break;
  87.                 case 21 : y2 = atof(str); break;
  88.                 case 31 : z2 = atof(str); break;
  89.                 case 12 : x3 = atof(str); break;
  90.                 case 22 : y3 = atof(str); break;
  91.                 case 32 : z3 = atof(str); break;
  92.                 case 13 : x4 = atof(str); break;
  93.                 case 23 : y4 = atof(str); break;
  94.                 case 33 : z4 = atof(str); break;
  95.                 default : break;
  96.             }
  97.         } while(key != 33);
  98.  
  99.         /* check for non-unique points */
  100.  
  101.         npnts = 4;
  102.         if(x1==x2 && y1==y2 && z1==z2) {
  103.             x2 = x3; x3 = x4;
  104.             y2 = y3; y3 = y4;
  105.             z2 = z3; z3 = z4;
  106.             npnts--;
  107.         }
  108.         if(npnts < 3) continue;
  109.         if(x1==x2 && y1==y2 && z1==z2) {
  110.             continue;
  111.         }
  112.         if(x1==x3 && y1==y3 && z1==z3) {
  113.             x3 = x4;
  114.             y3 = y4;
  115.             z3 = z4;
  116.             npnts--;
  117.         }
  118.         if(npnts < 3) continue;
  119.         if(x2==x3 && y2==y3 && z2==z3) {
  120.             x3 = x4;
  121.             y3 = y4;
  122.             z3 = z4;
  123.             npnts--;
  124.         }
  125.         if(npnts < 3) continue;
  126.         if(npnts==4 && x4==x3 && y4==y3 && z4==z3) {
  127.             npnts--;
  128.         }
  129.         if(npnts < 3) continue;
  130.  
  131.         /* got the poly, output it */
  132.  
  133.         if(npnts == 4) {
  134.         fprintf(raw_out_file,"%.4f %.4f %.4f\n", x1, y1, z1);
  135.         fprintf(raw_out_file,"%.4f %.4f %.4f\n", x2, y2, z2);
  136.         fprintf(raw_out_file,"%.4f %.4f %.4f\n}\n", x3, y3, z3);
  137.         fprintf(raw_out_file,"%.4f %.4f %.4f\n", x1, y1, z1);
  138.         fprintf(raw_out_file,"%.4f %.4f %.4f\n", x3, y3, z3);
  139.         fprintf(raw_out_file,"%.4f %.4f %.4f\n}\n", x4, y4, z4);
  140.         } else {
  141.         fprintf(raw_out_file,"%.4f %.4f %.4f\n", x1, y1, z1);
  142.         fprintf(raw_out_file,"%.4f %.4f %.4f\n", x2, y2, z2);
  143.         fprintf(raw_out_file,"%.4f %.4f %.4f\n}\n", x3, y3, z3);
  144.     }   /* END WHILE */
  145.  
  146.    }
  147.  
  148. fclose(raw_out_file);
  149. fclose(dxf_in_file);
  150. }       /* end of main() */
  151.