home *** CD-ROM | disk | FTP | other *** search
/ Computerworld 1996 March / Computerworld_1996-03_cd.bin / idg_cd3 / aplikace / cad / wplot211 / dlldemo.c < prev    next >
C/C++ Source or Header  |  1995-09-16  |  5KB  |  138 lines

  1. /* DLLDEMO.C - Demo of using WPLOTCMD.DLL to send commands & data to WPLOT. */
  2. /* See header file WPLOTCMD.H for comments on using WPLOTCMD.DLL functions. */
  3. /* DLLDEMO was written so that it could be compiled as an "EasyWin" program */
  4. /* using Borland C++ or as a "QuickWin" program using Visual C++.  The      */
  5. /* program must be linked with WPLOTCMD.LIB or must have a DEF file         */
  6. /* containing the following lines:  IMPORTS  WPLOTCMD.StartWPlot            */
  7. /*                                           WPLOTCMD.WPlotcmd              */
  8. /* Programmer: William G. Hood, 9-16-95  */
  9. #include <stdio.h>
  10. #include <conio.h>
  11. #include <windows.h>
  12. #include <math.h>
  13. #include <stdlib.h>
  14. #include <time.h>
  15. #include "wplotcmd.h"
  16.  
  17. #define ESC 27
  18. #define ID   1
  19.  
  20. int send( int id, char *pcmd );
  21. void SetWinSize( HWND hwind, int lines );
  22.  
  23. void main( void )
  24.   {
  25.   HWND hwind, hwind2;
  26.   int i, errcode;
  27.   char buffer[81];
  28.   char *ErrMes[5] = {"Out of memory", "Invalid ID", "File WPLOT.EXE not found",
  29.                      "Path to WPLOT.EXE not found", "Unspecified Error" };
  30.   double x, y, ydata[9] = { 125, 175, 350, 750, 900, 750, 350, 175, 125 };
  31.  
  32.   printf(
  33.     "\n WPlot can be used to plot data collected, calculated, or\n"
  34.     " processed by a user written C or Visual Basic program.\n");
  35.   printf(
  36.     "\n This is an example of a program that plots data by sending\n"
  37.     " the data and plot commands to WPlot using functions in the\n"
  38.     " Dynamic Link Library WPLOTCMD.DLL; see the C source code in\n"
  39.     " file DLLDEMO.C or the Visual Basic source code in files\n"
  40.     " VBDEMO.FRM and VBDEMO.MAK.\n");
  41.  
  42.   printf("\n Press the ENTER key to start WPlot. ");
  43.   while( getchar() != '\n' );   /* Wait till ENTER key pressed */
  44.  
  45.   /* Now that it's sure that client window is the active one, get its handle */
  46.   hwind = GetActiveWindow();
  47.   SetWinSize( hwind, 13 );
  48.   hwind2 = GetFocus(); SetWinSize( hwind2, 9 );  /* Needed if using VC++ */
  49.  
  50.   errcode = StartWPlot( ID, NULL );   /* Launch WPlot using default path */
  51.   if( errcode > 0 )
  52.     { printf("ERROR: %s\n", ErrMes[ errcode < 5 ? errcode-1 : 4 ] ); return; }
  53.  
  54.   send( ID, "winpos 0.5 45.0" );  /* Set Window position */
  55.   send( ID, "plotsize 1" );    /* Set small plot size */
  56.   if( send( ID, "" ) )         /* Check that first command was received */
  57.     { printf("ERROR: WPlot is not responding." ); return; }
  58.  
  59.   printf("\n Press the ENTER key to see a plot of data sent to WPlot. ");
  60.   while( getchar() != '\n' );   /* Wait till ENTER key pressed */
  61.  
  62.   send( ID, "title Demo of Controlling WPlot" );
  63.   send( ID, "xlabel X-Axis" );
  64.   send( ID, "ylabel Y-Axis" );
  65.   send( ID, "xstart 0" );  send( ID, "xend 10" );    send( ID, "xstep 1" );
  66.   send( ID, "ystart 0" );  send( ID, "yend 1000" );  send( ID, "ystep 200" );
  67.   send( ID, "grids" );
  68.   send( ID, "read 9" );
  69.   for( i = 0; i < 9; ++i )
  70.     {
  71.     sprintf( buffer, "%d %f", i+1, ydata[i] );
  72.     send( ID, buffer );   /* Data points can be sent after a READ command. */
  73.     }
  74.   send( ID, "curve 1" );  /* The data set will be plotted as a smooth curve.*/
  75.   send( ID, "symbols" );  /* A symbol will be plotted at each data point.   */
  76.   send( ID, "plot" );     /* Plot previously read data points.              */
  77.  
  78.   printf("\n Press the ENTER key to plot data points as they are calculated. ");
  79.   while( getchar() != '\n' );   /* Wait till ENTER key pressed */
  80.  
  81.   /* Some special commands were added to WPlot for real-time data display: */
  82.   /* rtplot         - Displays axes, title and labels even if no data.     */
  83.   /* rtdata   m     - Allocates memory for a maximum of m data points.     */
  84.   /* rtpoint  x  y  - Inputs and plots point (x,y).                        */
  85.   /* rtend          - Terminates real-time plot & frees Windows resources. */
  86.   send( ID, "rtdata 51" );
  87.   for( i = 0; i < 51; ++i )
  88.     {
  89.     x = i / 5.0;
  90.     y = x * x * x + 2.0 * x * x - 30.0 * x + 90.0;
  91.     y += 20.0 * ( 2.0 * rand() - RAND_MAX ) / RAND_MAX;
  92.     printf(" x = %7.2f,  y = %7.2f\n", x, y );
  93.     sprintf( buffer, "rtpoint %7.2f %7.2f", x, y );
  94.     send( ID, buffer );
  95.     }
  96.   send( ID, "rtend" );
  97.  
  98.   printf("\nPress the ENTER key to make a polynomial curve fit. ");
  99.   while( getchar() != '\n' );   /* Wait till ENTER key pressed */
  100.  
  101.   send( ID, "poly 3" );
  102.   send( ID, "title Polynomial Curve Fit" );
  103.   send( ID, "plot" );
  104.  
  105.   printf("\nPress the ENTER key to exit. ");
  106.   while( getchar() != '\n' );   /* Wait till ENTER key pressed */
  107.  
  108.   send( ID, "exit" );
  109.   DestroyWindow( hwind );
  110.   }
  111.  
  112. int send( int id, char *pcmd )
  113. /* Sends command pointed to by pcmd to WPLOTCMD.DLL using buffer #id.      */
  114. /* The function keeps trying until the DLL is ready to accept the command. */
  115. /* Returns 0 on success or 1 if times out after 30 seconds.                */
  116.   {
  117.   long tstart;
  118.  
  119.   tstart = time(NULL);
  120.   while( 1 )
  121.     {
  122.     if( WPlotcmd( id, pcmd, 0 ) == 0 ) return 0;
  123.     if( time(NULL) - tstart > 30 ) break;
  124.     }
  125.   printf("ERROR: send function timed out.\n");
  126.   return 1;
  127.   }
  128.  
  129. void SetWinSize( HWND hwind, int lines )
  130. /* Sets client window's position and sets size to specified number of lines */
  131.   {
  132.   int nc = GetSystemMetrics( SM_CYCAPTION );
  133.   RECT rect;
  134.  
  135.   GetWindowRect( hwind, (LPRECT) &rect );
  136.   MoveWindow( hwind, 1, 1, rect.right-rect.left , lines * nc, TRUE );
  137.   }
  138.