home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 329.lha / MultiPlot / source / file.c < prev    next >
C/C++ Source or Header  |  1990-01-05  |  15KB  |  480 lines

  1. #include <graphics/display.h>
  2. #include <libraries/dosextens.h>
  3. #include <libraries/diskfont.h>
  4. #include <exec/exec.h>
  5. #include <intuition/intuitionbase.h>
  6. #include <graphics/regions.h>
  7. #include <devices/keymap.h>
  8. #include <stdio.h>
  9. #include <workbench/startup.h>
  10. #include <graphics/gfxmacros.h>
  11. #include <graphics/gfxbase.h>
  12. #include <math.h>
  13.  
  14. #include "diskicon.h"
  15. #include "struct.h"
  16. #include "plotlim.h"
  17.  
  18. extern int MAXVERT;
  19.  
  20. extern int GetDat();
  21. extern void plot();
  22. extern struct Screen *screen;
  23. extern struct NewScreen newscreen;
  24. extern struct NewWindow newwindow;
  25. extern struct Window *window;
  26. extern int XMINP;      /* LMARGIN */
  27. extern int YMAXP;   /* MAXVERT - TMARGIN */
  28. extern int YMINP;    /* BMARGIN */
  29.  
  30.  
  31.  
  32. #define QUIT 0
  33. #define GO 1
  34. extern int QuitFlag;
  35.  
  36. extern long IconBase;
  37.  
  38. extern FFP xtic[MAXTICS],ytic[MAXTICS];
  39. extern short xticp[MAXTICS],yticp[MAXTICS];
  40. extern char filename[150];
  41. extern struct RastPort *p;
  42. extern struct ViewPort *vp;
  43.  
  44. #define P_XREG 10000
  45. #define P_YREG 10000
  46. #define P_X_TIC_SIZE X_TIC_SIZE*(P_YREG/MAXVERT)
  47. #define P_Y_TIC_SIZE Y_TIC_SIZE*(P_XREG/MAXHORIZ)
  48. #define P_CH_WID 75
  49. #define P_CH_HEI 150
  50. #define P_LMARGIN P_CH_WID*8
  51. #define P_BMARGIN P_CH_HEI*2
  52. #define P_TMARGIN 0
  53. #define P_RMARGIN 0
  54.  
  55. static FFP XScale, YScale, XOffset, YOffset;
  56.  
  57. void fWrtFFP(fp, ffpval)
  58. FILE *fp;
  59. FFP ffpval;
  60. {
  61.    fprintf(fp, "%d", (int)ffpval);
  62. }
  63.  
  64.  
  65. void WrtAxes(fp, Tics, Grid)
  66. FILE *fp;
  67. struct Tics *Tics;
  68. short Grid;
  69. {
  70.    short n, tmpx, tmpy, i;
  71.    FFP *x, *y;
  72.    char tmpstr[20];
  73.  
  74.    /*** DRAW BORDER ***/
  75.    fprintf(fp,"SP1;PU%d %d;",P_LMARGIN,P_BMARGIN);
  76.    fprintf(fp,"PD%d %d %d %d %d %d %d %d;\n",P_XREG,P_BMARGIN,P_XREG,P_YREG,
  77.    P_LMARGIN,P_YREG,P_LMARGIN,P_BMARGIN);
  78.  
  79.    /*** DRAW TICS/GRID ***/
  80.    fprintf(fp,"SP2;");
  81.    x=Tics->x; y=Tics->y;
  82.    for (i=1; i < Tics->NX; i++) {
  83.       if (Grid) {
  84.          fprintf(fp,"PU"); fWrtFFP(fp,(XScale * (XOffset + x[i])));
  85.          fprintf(fp," %d;PD",P_BMARGIN);
  86.          fWrtFFP(fp,(XScale * (XOffset + x[i])));
  87.          fprintf(fp," %d;\n",P_YREG);
  88.       }
  89.       else {
  90.          fprintf(fp,"PU"); fWrtFFP(fp,(XScale * (XOffset + x[i])));
  91.          fprintf(fp," %d;PD",P_BMARGIN);
  92.          fWrtFFP(fp,(XScale * (XOffset + x[i])));
  93.          fprintf(fp," %d;",P_BMARGIN + P_X_TIC_SIZE);
  94.          fprintf(fp,"PU"); fWrtFFP(fp,(XScale * (XOffset + x[i])));
  95.          fprintf(fp," %d;PD",P_YREG);
  96.          fWrtFFP(fp,(XScale * (XOffset + x[i])));
  97.          fprintf(fp," %d;\n",P_YREG - P_X_TIC_SIZE);
  98.       }
  99.    }
  100.    for (i=1; i < Tics->NY; i++) {
  101.       if (Grid) {
  102.          fprintf(fp,"PU%d ",P_LMARGIN);
  103.          fWrtFFP(fp,(YScale * (YOffset + y[i])));
  104.          fprintf(fp,"PD%d ",P_XREG);
  105.          fWrtFFP(fp,(YScale * (YOffset + y[i])));
  106.          fprintf(fp,";\n");
  107.       }
  108.       else {
  109.          fprintf(fp,"PU%d ",P_LMARGIN);
  110.          fWrtFFP(fp,(YScale * (YOffset + y[i])));
  111.          fprintf(fp,";PD%d ", P_LMARGIN + P_Y_TIC_SIZE);
  112.          fWrtFFP(fp,(YScale * (YOffset + y[i])));
  113.          fprintf(fp,";");
  114.          fprintf(fp,"PU%d ",P_XREG);
  115.          fWrtFFP(fp,(YScale * (YOffset + y[i])));
  116.          fprintf(fp,";PD%d ", P_XREG - P_Y_TIC_SIZE);
  117.          fWrtFFP(fp,(YScale * (YOffset + y[i])));
  118.          fprintf(fp,";\n");
  119.       }
  120.    }
  121.  
  122.    /*** PRINT TIC VALUES ***/
  123.    fprintf(fp,"SP1;");
  124.    for (i=0; i < Tics->NX; i++) {
  125.       n = sprintf(tmpstr, "%-.4f", Tics->x[i]);
  126.       n = min(n, 7);
  127.       while (tmpstr[n-1] == '0') n--;
  128.       tmpstr[n]=0;
  129.       tmpx = (short)((XScale * (XOffset + Tics->x[i])));
  130.       tmpx -= (((n+1)/2)-.5) * P_CH_WID;
  131.       fprintf(fp, "PU%d 1;", tmpx);
  132.       fprintf(fp,"LB%s\003", tmpstr);
  133.    }
  134.    for (i=0; i < Tics->NY; i++) {
  135.       n = sprintf(tmpstr, "%-.4f", Tics->y[i]);
  136.       n = min(n, 7);
  137.       while (tmpstr[n-1] == '0') n--;
  138.       tmpstr[n]=0;
  139.       tmpy = (short)((YScale * (YOffset + Tics->y[i])));
  140.       tmpy -= .5 * P_CH_WID;
  141.       fprintf(fp, "PU1 %d;", tmpy);
  142.       fprintf(fp,"LB%s\003", tmpstr);
  143.    }
  144. }
  145.  
  146. extern USHORT chip WaitSprite[];
  147. extern char *stpchr();
  148.  
  149. WrtPlt(Pict,Dest)
  150. USHORT Dest;
  151. struct Pict *Pict;
  152. {
  153.    struct Process  *OurTask;
  154.    struct Window   *old_pr_WindowPtr;
  155.    FILE *fp;
  156.    short i;
  157.    struct Plot *Plot;
  158.    FFP *x, *y, *e;
  159.    short xptsiz, yptsiz;
  160.    static char plotname[150], def_drive[150]="PLT:", def_path[100], def_node[30],def_extn[20];
  161.    static char PlotNum = 0;
  162.  
  163.    if (Dest)
  164.       {
  165.           strcpy(plotname,filename);
  166.           strsfn(plotname,def_drive,def_path,def_node,def_extn);
  167.           strcat(def_drive,def_path);
  168.           strcat(def_node,".plt");
  169.           strcat(def_node,PlotNum);
  170.  
  171.           OurTask = (struct Process *)FindTask(0L);
  172.           old_pr_WindowPtr = (struct Window *)OurTask->pr_WindowPtr;
  173.           OurTask->pr_WindowPtr = (APTR)window;
  174.           if (get_fname(window,screen,"Save File As...",def_node,def_drive)==NULL)
  175.                {
  176.                   OurTask->pr_WindowPtr = (APTR)old_pr_WindowPtr;
  177.                   return(TRUE);
  178.                 }
  179.  
  180.            OurTask->pr_WindowPtr = (APTR)old_pr_WindowPtr;
  181.            strmfp(plotname,def_drive,def_node);
  182.            PutDiskObject(plotname,&IconDiskObject);
  183.        }
  184.    else
  185.        {
  186.           if (!(fp = fopen("l:plt-handler","r") ))
  187.              {
  188.                 Message("Printing needs the PLT: Device"); return(FALSE);
  189.              }
  190.           fclose(fp);
  191.           Execute("c:mount plt:",0,0);
  192.           strcpy(plotname,"PLT:");
  193.        }
  194.  
  195.    if (!(fp = fopen(plotname,"w") ))
  196.       {Message("     Can't open file        "); return(FALSE);}
  197.  
  198.    SetPointer(window,WaitSprite,26,14,-4,-4);
  199.  
  200.    fprintf(fp,"IN;SC 0 %d 0 %d;\n", P_XREG+100, P_YREG+100);
  201.    fprintf(fp,"VS 20.0;\n");  /* VELOCITY cm/s */
  202.    fprintf(fp,"\033.N;19:\033.I81;;17:\n"); /* XON/XOFF */
  203.    XScale = (FFP)(P_XREG - P_LMARGIN - P_RMARGIN) /
  204.             (Pict->CurrReg->XMax - Pict->CurrReg->XMin);
  205.    YScale = (FFP)(P_YREG - P_BMARGIN - P_TMARGIN) /
  206.             (Pict->CurrReg->YMax - Pict->CurrReg->YMin);
  207.    XOffset = (FFP)(P_LMARGIN) / XScale - Pict->CurrReg->XMin;
  208.    YOffset = (FFP)(P_BMARGIN) / YScale - Pict->CurrReg->YMin;
  209.  
  210.    Plot = Pict->Plot;
  211.    while (Plot) {
  212.       if (Plot->Enabled) {
  213.          fprintf(fp,"PU;SP%d;\n", 1+Plot->Color-PLOTCOLORBASE);
  214.  
  215.          /* PLOT LINES */
  216.          if (Plot->PointSize <= 0) {
  217.             x=Plot->x; y=Plot->y;
  218.             fprintf(fp, "PU"); fWrtFFP(fp,(XScale * (XOffset + *x)));
  219.             fprintf(fp," ");
  220.             fWrtFFP(fp,(YScale * (YOffset + *y))); fprintf(fp,";");
  221.             x++; y++;
  222.             fprintf(fp, "PD");
  223.             for (i=1; i<Plot->NPts; i++, x++, y++) {
  224.                if (i>1) fprintf(fp, " ");
  225.                fWrtFFP(fp,(XScale * (XOffset + *x)));
  226.                fprintf(fp," ");
  227.                fWrtFFP(fp,(YScale * (YOffset + *y)));
  228.             }
  229.             fprintf(fp,";\n");
  230.          }
  231.  
  232.          /* PLOT POINTS */
  233.          if (Plot->PointSize != 0) {
  234.             xptsiz = abs(Plot->PointSize) * (P_XREG / 1100);
  235.             yptsiz = abs(Plot->PointSize) * (P_YREG / 850);
  236.             x=Plot->x; y=Plot->y;
  237.             for (i=0; i<Plot->NPts; i++, x++, y++) {
  238.                fprintf(fp,"PU");
  239.                fWrtFFP(fp,(XScale * (XOffset + *x)));
  240.                fprintf(fp," ");
  241.                fWrtFFP(fp,(YScale * (YOffset + *y)));
  242.                fprintf(fp,";");
  243.                fprintf(fp,"PR%d %d;", -xptsiz/2, -yptsiz/2);
  244.                fprintf(fp,"PD%d 0 0 %d %d 0 0 %d;PA;\n",
  245.                      xptsiz, yptsiz, -xptsiz, -yptsiz);
  246.             }
  247.          }
  248.  
  249.          /* PLOT ERROR BARS */
  250.          if (Pict->ShowErr) {
  251.             x=Plot->x; y=Plot->y; e=Plot->e;
  252.             for (i=0; i<Plot->NPts; i++, x++, y++, e++) {
  253.                fprintf(fp,"PU");
  254.                fWrtFFP(fp,(XScale * (XOffset + *x)));
  255.                fprintf(fp," ");
  256.                fWrtFFP(fp,(YScale * (YOffset + (*y - *e))));
  257.                fprintf(fp,";PD");
  258.                fWrtFFP(fp,(XScale * (XOffset + *x)));
  259.                fprintf(fp," ");
  260.                fWrtFFP(fp,(YScale * (YOffset + (*y + *e))));
  261.                fprintf(fp,";\n");
  262.             }
  263.          }
  264.       }
  265.       Plot = Plot->NextPlot;
  266.    }
  267.  
  268.    fprintf(fp,"SR%f %f;\n",
  269.          (float)(100*P_CH_WID) / (P_XREG - P_LMARGIN - P_RMARGIN),
  270.          (float)(100*P_CH_HEI) / (P_YREG - P_BMARGIN - P_TMARGIN));
  271.    WrtAxes(fp, Pict->Tics, Pict->Grid);
  272.  
  273.    fprintf(fp, "SP;");
  274.    (void) fclose(fp);
  275.    PlotNum++;
  276.    ClearPointer(window);
  277.    return(TRUE);
  278. }
  279.  
  280.  
  281.  
  282.  
  283. void header(n, c, t, fp)
  284. int n, c, t;
  285. FILE *fp;
  286. {
  287.    UBYTE bdummy;
  288.    bdummy = n;
  289.    fwrite((char *)&bdummy, 1, 1, fp);
  290.    bdummy = c;
  291.    fwrite((char *)&bdummy, 1, 1, fp);
  292.    bdummy = 0;
  293.    fwrite((char *)&bdummy, 1, 1, fp); /* LineType */
  294.    bdummy = t;
  295.    fwrite((char *)&bdummy, 1, 1, fp);
  296.    bdummy = 0;
  297.    fwrite((char *)&bdummy, 1, 1, fp); /* padding for future use */
  298.    fwrite((char *)&bdummy, 1, 1, fp);
  299. }
  300.  
  301. To_mCAD(Pict,Dest)
  302. struct Pict *Pict;
  303. USHORT Dest;
  304. {
  305.  
  306.    struct Process  *OurTask;
  307.    struct Window   *old_pr_WindowPtr;
  308.  
  309.    FILE *fp;
  310.    short i;
  311.    struct Plot *Plot;
  312.    FFP *x, *y, *e, xtmp, ytmp, xtmp1, ytmp1;
  313.    FFP xptsiz, yptsiz, XReg, YReg, XScale, XOffset;
  314.    char plotname[150], def_drive[150], def_path[100], def_node[30],def_extn[20];
  315.    static char mPlotNum = 0;
  316.  
  317.   if (Dest)
  318.     {
  319.      strcpy(plotname,filename);
  320.      strsfn(plotname,def_drive,def_path,def_node,def_extn);
  321.      strcat(def_drive,def_path);
  322.      strcat(def_node,".txt");
  323.      strcat(def_node,mPlotNum);
  324.  
  325.            OurTask = (struct Process *)FindTask(0L);
  326.            old_pr_WindowPtr = (struct Window *)OurTask->pr_WindowPtr;
  327.            OurTask->pr_WindowPtr = (APTR)window;
  328.            if (get_fname(window,screen,"Save File As...",def_node,def_drive)==NULL)
  329.                {
  330.                   OurTask->pr_WindowPtr = (APTR)old_pr_WindowPtr;
  331.                   return(TRUE);
  332.                 }
  333.  
  334.      OurTask->pr_WindowPtr = (APTR)old_pr_WindowPtr;
  335.      strmfp(plotname,def_drive,def_node);
  336.      PutDiskObject(plotname,&IconDiskObject);
  337.     }
  338.    else
  339.        {
  340.           if (!(fp = fopen("T:tempfile","w") ))
  341.              {
  342.                 Message("Save needs the T: Device"); return(FALSE);
  343.              }
  344.           fclose(fp);
  345.           strcpy(plotname,"T:tempfile");
  346.        }
  347.  
  348.    if (!(fp = fopen(plotname,"w") ))
  349.       {Message("     Can't open file        "); return(FALSE);}
  350.  
  351.    XReg = Pict->CurrReg->XMax - Pict->CurrReg->XMin;
  352.    YReg = Pict->CurrReg->YMax - Pict->CurrReg->YMin;
  353.    XScale = YReg/XReg * 1.4434; XOffset = Pict->CurrReg->XMin;
  354.  
  355.    Plot = Pict->Plot;
  356.    while (Plot) {
  357.       if (Plot->Enabled) {
  358.  
  359.          /* PLOT LINES */
  360.          if (Plot->PointSize <= 0) {
  361.             x=Plot->x; y=Plot->y;
  362.  /*--->>*/  for (i=0; i<Plot->NPts; i++, x++, y++) {
  363.                xtmp = XScale * (*x - XOffset);
  364.                fprintf(fp, "%f %f\n", xtmp, *y);
  365.             }
  366.             fprintf(fp,"*>\n*C %d\n\n", 1+Plot->Color-PLOTCOLORBASE);
  367.          }
  368.  
  369.          /* PLOT POINTS */
  370.          if (Plot->PointSize != 0) {
  371.             xptsiz = abs(Plot->PointSize) * (XReg / 582.);
  372.             xptsiz = XScale * xptsiz;
  373.             yptsiz = abs(Plot->PointSize) * (YReg / 360.);
  374.             x=Plot->x; y=Plot->y;
  375.             for (i=0; i<Plot->NPts; i++, x++, y++) {
  376.                xtmp = XScale * (*x - XOffset);
  377.                xtmp -= xptsiz/2;
  378.                ytmp = *y - yptsiz/2;
  379.                fprintf(fp,"%f %f\n", xtmp, ytmp); xtmp += xptsiz;
  380.                fprintf(fp,"%f %f\n", xtmp, ytmp); ytmp += yptsiz;
  381.                fprintf(fp,"%f %f\n", xtmp, ytmp); xtmp -= xptsiz;
  382.                fprintf(fp,"%f %f\n", xtmp, ytmp); ytmp -= yptsiz;
  383.                fprintf(fp,"%f %f\n", xtmp, ytmp);
  384.                fprintf(fp,"*>\n*C %d\n\n", 1+Plot->Color-PLOTCOLORBASE);
  385.             }
  386.          }
  387.  
  388.          /* PLOT ERROR BARS */
  389.          if (Pict->ShowErr) {
  390.             x=Plot->x; y=Plot->y; e=Plot->e;
  391.             for (i=0; i<Plot->NPts; i++, x++, y++, e++) {
  392.                xtmp = XScale * (*x - XOffset);
  393.                ytmp = *y - *e;
  394.                fprintf(fp,"%f %f\n", xtmp, ytmp);
  395.                ytmp = *y + *e;
  396.                fprintf(fp,"%f %f\n", xtmp, ytmp);
  397.                fprintf(fp,"*>\n*C %d\n\n", 1+Plot->Color-PLOTCOLORBASE);
  398.             }
  399.          }
  400.       }
  401.       Plot = Plot->NextPlot;
  402.    }
  403.  
  404.    /*** DRAW TICS/GRID ***/
  405.    x = Pict->Tics->x; y = Pict->Tics->y;
  406.    for (i=1; i < Pict->Tics->NX; i++) {
  407.       xtmp = XScale * (x[i] - XOffset);
  408.       if (Pict->Grid) {
  409.          PToU(Pict, 0, YMINP, &xtmp, &ytmp);
  410.          PToU(Pict, 0, YMAXP, &xtmp1, &ytmp1);
  411.          fprintf(fp,"%f %f\n%f %f\n*>\n*C 2\n\n", xtmp, ytmp, xtmp, ytmp1);
  412.       }
  413.       else {
  414.          PToU(Pict, 0, YMINP, &xtmp1, &ytmp);
  415.          fprintf(fp,"%f %f\n", xtmp, ytmp);
  416.          PToU(Pict, 0, YMINP+X_TIC_SIZE, &xtmp1, &ytmp1);
  417.          fprintf(fp,"%f %f\n*>\n*C 3\n\n", xtmp, ytmp1);
  418.          PToU(Pict, 0, YMAXP, &xtmp1, &ytmp);
  419.          fprintf(fp,"%f %f\n", xtmp, ytmp);
  420.          PToU(Pict, 0, YMAXP-X_TIC_SIZE, &xtmp1, &ytmp1);
  421.          fprintf(fp,"%f %f\n*>\n*C 3\n\n", xtmp, ytmp1);
  422.       }
  423.    }
  424.    for (i=1; i < Pict->Tics->NY; i++) {
  425.       if (Pict->Grid) {
  426.          PToU(Pict, XMINP, YMINP, &xtmp, &ytmp);
  427.          xtmp = XScale * (xtmp - XOffset);
  428.          PToU(Pict, XMAXP, YMAXP, &xtmp1, &ytmp1);
  429.          xtmp1 = XScale * (xtmp1 - XOffset);
  430.          fprintf(fp,"%f %f\n%f %f\n*>\n*C 2\n\n", xtmp, y[i], xtmp1, y[i]);
  431.       }
  432.       else {
  433.          PToU(Pict, XMINP, 0, &xtmp, &ytmp);
  434.          xtmp = XScale * (xtmp - XOffset);
  435.          fprintf(fp,"%f %f\n", xtmp, y[i]);
  436.          PToU(Pict, XMINP+Y_TIC_SIZE, 0, &xtmp1, &ytmp1);
  437.          xtmp1 = XScale * (xtmp1 - XOffset);
  438.          fprintf(fp,"%f %f\n*>\n*C 3\n\n", xtmp1, y[i]);
  439.          PToU(Pict, XMAXP, 0, &xtmp, &ytmp);
  440.          xtmp = XScale * (xtmp - XOffset);
  441.          fprintf(fp,"%f %f\n", xtmp, y[i]);
  442.          PToU(Pict, XMAXP-Y_TIC_SIZE, 0, &xtmp1, &ytmp1);
  443.          xtmp1 = XScale * (xtmp1 - XOffset);
  444.          fprintf(fp,"%f %f\n*>\n*C 3\n\n", xtmp1, y[i]);
  445.       }
  446.    }
  447.  
  448.    /*** PRINT TIC VALUES ***/
  449. /*
  450. *   for (i=0; i < Pict->Tics->NX; i++) {
  451. *      n = sprintf(tmpstr, "%-.4f", Pict->Tics->x[i]);
  452. *      n = min(n, 7);
  453. *      while (tmpstr[n-1] == '0') n--;
  454. *      tmpstr[n]=0;
  455. *   }
  456. *   for (i=0; i < Pict->Tics->NY; i++) {
  457. *      n = sprintf(tmpstr, "%-.4f", Pict->Tics->y[i]);
  458. *      n = min(n, 7);
  459. *      while (tmpstr[n-1] == '0') n--;
  460. *      tmpstr[n]=0;
  461. *   }
  462. */
  463.  
  464.    /*** DRAW AXES ***/
  465.    PToU(Pict, XMINP, YMINP, &xtmp, &ytmp);
  466.    xtmp = XScale * (xtmp - XOffset);
  467.    PToU(Pict, XMAXP, YMAXP, &xtmp1, &ytmp1);
  468.    xtmp1 = XScale * (xtmp1 - XOffset);
  469.    fprintf(fp, "%f %f\n", xtmp, ytmp);
  470.    fprintf(fp, "%f %f\n", xtmp1, ytmp);
  471.    fprintf(fp, "%f %f\n", xtmp1, ytmp1);
  472.    fprintf(fp, "%f %f\n", xtmp, ytmp1);
  473.    fprintf(fp, "%f %f\n", xtmp, ytmp);
  474.    fprintf(fp,"*C 1\n\n");
  475.  
  476.    (void) fclose(fp);
  477.    mPlotNum++;
  478.    return(TRUE);
  479. }
  480.