home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Computerworld 1996 March
/
Computerworld_1996-03_cd.bin
/
idg_cd3
/
aplikace
/
cad
/
wplot211
/
dlldemo.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-09-16
|
5KB
|
138 lines
/* DLLDEMO.C - Demo of using WPLOTCMD.DLL to send commands & data to WPLOT. */
/* See header file WPLOTCMD.H for comments on using WPLOTCMD.DLL functions. */
/* DLLDEMO was written so that it could be compiled as an "EasyWin" program */
/* using Borland C++ or as a "QuickWin" program using Visual C++. The */
/* program must be linked with WPLOTCMD.LIB or must have a DEF file */
/* containing the following lines: IMPORTS WPLOTCMD.StartWPlot */
/* WPLOTCMD.WPlotcmd */
/* Programmer: William G. Hood, 9-16-95 */
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include "wplotcmd.h"
#define ESC 27
#define ID 1
int send( int id, char *pcmd );
void SetWinSize( HWND hwind, int lines );
void main( void )
{
HWND hwind, hwind2;
int i, errcode;
char buffer[81];
char *ErrMes[5] = {"Out of memory", "Invalid ID", "File WPLOT.EXE not found",
"Path to WPLOT.EXE not found", "Unspecified Error" };
double x, y, ydata[9] = { 125, 175, 350, 750, 900, 750, 350, 175, 125 };
printf(
"\n WPlot can be used to plot data collected, calculated, or\n"
" processed by a user written C or Visual Basic program.\n");
printf(
"\n This is an example of a program that plots data by sending\n"
" the data and plot commands to WPlot using functions in the\n"
" Dynamic Link Library WPLOTCMD.DLL; see the C source code in\n"
" file DLLDEMO.C or the Visual Basic source code in files\n"
" VBDEMO.FRM and VBDEMO.MAK.\n");
printf("\n Press the ENTER key to start WPlot. ");
while( getchar() != '\n' ); /* Wait till ENTER key pressed */
/* Now that it's sure that client window is the active one, get its handle */
hwind = GetActiveWindow();
SetWinSize( hwind, 13 );
hwind2 = GetFocus(); SetWinSize( hwind2, 9 ); /* Needed if using VC++ */
errcode = StartWPlot( ID, NULL ); /* Launch WPlot using default path */
if( errcode > 0 )
{ printf("ERROR: %s\n", ErrMes[ errcode < 5 ? errcode-1 : 4 ] ); return; }
send( ID, "winpos 0.5 45.0" ); /* Set Window position */
send( ID, "plotsize 1" ); /* Set small plot size */
if( send( ID, "" ) ) /* Check that first command was received */
{ printf("ERROR: WPlot is not responding." ); return; }
printf("\n Press the ENTER key to see a plot of data sent to WPlot. ");
while( getchar() != '\n' ); /* Wait till ENTER key pressed */
send( ID, "title Demo of Controlling WPlot" );
send( ID, "xlabel X-Axis" );
send( ID, "ylabel Y-Axis" );
send( ID, "xstart 0" ); send( ID, "xend 10" ); send( ID, "xstep 1" );
send( ID, "ystart 0" ); send( ID, "yend 1000" ); send( ID, "ystep 200" );
send( ID, "grids" );
send( ID, "read 9" );
for( i = 0; i < 9; ++i )
{
sprintf( buffer, "%d %f", i+1, ydata[i] );
send( ID, buffer ); /* Data points can be sent after a READ command. */
}
send( ID, "curve 1" ); /* The data set will be plotted as a smooth curve.*/
send( ID, "symbols" ); /* A symbol will be plotted at each data point. */
send( ID, "plot" ); /* Plot previously read data points. */
printf("\n Press the ENTER key to plot data points as they are calculated. ");
while( getchar() != '\n' ); /* Wait till ENTER key pressed */
/* Some special commands were added to WPlot for real-time data display: */
/* rtplot - Displays axes, title and labels even if no data. */
/* rtdata m - Allocates memory for a maximum of m data points. */
/* rtpoint x y - Inputs and plots point (x,y). */
/* rtend - Terminates real-time plot & frees Windows resources. */
send( ID, "rtdata 51" );
for( i = 0; i < 51; ++i )
{
x = i / 5.0;
y = x * x * x + 2.0 * x * x - 30.0 * x + 90.0;
y += 20.0 * ( 2.0 * rand() - RAND_MAX ) / RAND_MAX;
printf(" x = %7.2f, y = %7.2f\n", x, y );
sprintf( buffer, "rtpoint %7.2f %7.2f", x, y );
send( ID, buffer );
}
send( ID, "rtend" );
printf("\nPress the ENTER key to make a polynomial curve fit. ");
while( getchar() != '\n' ); /* Wait till ENTER key pressed */
send( ID, "poly 3" );
send( ID, "title Polynomial Curve Fit" );
send( ID, "plot" );
printf("\nPress the ENTER key to exit. ");
while( getchar() != '\n' ); /* Wait till ENTER key pressed */
send( ID, "exit" );
DestroyWindow( hwind );
}
int send( int id, char *pcmd )
/* Sends command pointed to by pcmd to WPLOTCMD.DLL using buffer #id. */
/* The function keeps trying until the DLL is ready to accept the command. */
/* Returns 0 on success or 1 if times out after 30 seconds. */
{
long tstart;
tstart = time(NULL);
while( 1 )
{
if( WPlotcmd( id, pcmd, 0 ) == 0 ) return 0;
if( time(NULL) - tstart > 30 ) break;
}
printf("ERROR: send function timed out.\n");
return 1;
}
void SetWinSize( HWND hwind, int lines )
/* Sets client window's position and sets size to specified number of lines */
{
int nc = GetSystemMetrics( SM_CYCAPTION );
RECT rect;
GetWindowRect( hwind, (LPRECT) &rect );
MoveWindow( hwind, 1, 1, rect.right-rect.left , lines * nc, TRUE );
}