/**************************************************************************/
/* The next few lines are standard "boiler plate" code all UDF's have     */
/**************************************************************************/

#include <windows.h>
#include <dde.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>

/* boiler plate declarations for functions in zclient we will call */
void WaitForData(HWND hwnd);
char *GetString(char *szBuffer, int n, char *szSubString);
void PostRequestMessage(char *szItem, char *szDataOut);
void CenterWindow(HWND hwnd);
void UserFunction(char *szCommandLine);
void MakeEmptyWindow(int text, char *szAppName, char *szOptions);

/* boiler plate global variables used by the client code */
extern HINSTANCE globalhInstance;
extern HWND hwndClient;


/**************************************************/
/* Below is the code specific to this UDF program */
/**************************************************/

// udo_demo sample program
// Written by Kenneth Moore July 1997
// Illustrates the simplest use of a User Defined Operand

/* okay, now the actual code that creates the UDO data for optimization by ZEMAX */
/* this function MUST be called UserFunction and take the command line argument      */

void UserFunction(char *szCommandLine)
{
static char szBuffer[5000], szSub[256];
double hx, hy, px, py;
int buffer_code;

/* extract the command line arguments */

/* the buffer code holds the "index" to the lens we need to evaluate the operand for */
buffer_code = atoi(GetString(szCommandLine, 0, szSub));
hx          = atof(GetString(szCommandLine, 1, szSub));
hy          = atof(GetString(szCommandLine, 2, szSub));
px          = atof(GetString(szCommandLine, 3, szSub));
py          = atof(GetString(szCommandLine, 4, szSub));


/* first, get the specified lens into the DDE server procedure */
/* this is a critical step, we must be sure we have the correct data generated by the optimizer */
/* there may be more than 1 simultaneous DDE conversation going on */
sprintf(szBuffer,"GetUDOSystem,%i", buffer_code);
PostRequestMessage(szBuffer, szBuffer);


/* Here is where you can place any code you like to compute anything on the new lens */


/* now simply return some dummy test data; these should show up in the MFE if the dataflag is 0, 1, 2, 3, 4... */
sprintf(szBuffer,"SetUDOData,%i,%.7E,%.7E,%.7E,%.7E,%.7E,%.7E", buffer_code, hx, hy, px, py, 12.5, 13.5);
//MessageBox (hwndClient, szBuffer, "Data to pass back!", MB_ICONEXCLAMATION | MB_OK);
PostRequestMessage(szBuffer, szBuffer);

/* the SetUDOData command will close out the buffer lens data, so it's the last thing we can do */
}

