⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zarraytrace.cpp

📁 zemax与matlab调用程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
         FreeDDElParam(WM_DDE_ACK, lParam);
         dummy = (HWND) wParam;
         if (uiLow) GlobalDeleteAtom((ATOM) uiLow);
         if (uiHi) GlobalDeleteAtom((ATOM) uiHi);
			}
   	return 0;

   case WM_DDE_TERMINATE :
   	PostMessage(hwndServer, WM_DDE_TERMINATE, (WPARAM) hwnd, 0L);
      hwndServer = NULL;
      return 0;

   case WM_PAINT :
   	{
      PAINTSTRUCT ps;
   	BeginPaint(hwnd, &ps);
      EndPaint(hwnd, &ps);
      }
      return 0;

   case WM_CLOSE :
   	PostMessage(hwndServer, WM_DDE_TERMINATE, (WPARAM) hwnd, 0L);
   	break;             // for default processing

   case WM_DESTROY :
   	PostQuitMessage(0);
      return 0;
   }
   return DefWindowProc(hwnd, iMsg, wParam, lParam);
}

void WaitForData(HWND hwnd)
{
int sleep_count;
MSG msg;
DWORD dwTime;
dwTime = GetCurrentTime();
GotData = 0;
sleep_count = 0;
#if(0)
while ( (GetCurrentTime() - dwTime < DDE_Timeout) && !GotData)
	{
   while (PeekMessage (&msg, hwnd, WM_DDE_FIRST, WM_DDE_LAST, PM_REMOVE))
   	{
      DispatchMessage (&msg);
      }
   /* Give the server a chance to respond */
   Sleep(0);
   }
#else

while (!GotData)
	{
   while (PeekMessage (&msg, hwnd, WM_DDE_FIRST, WM_DDE_LAST, PM_REMOVE))
   	{
      DispatchMessage (&msg);
      }
   /* Give the server a chance to respond */
   Sleep(0);
   sleep_count++;
   if (sleep_count > 10000)
   	{
      if (GetCurrentTime() - dwTime > DDE_Timeout) 
		{
		// The command has timed out
		mexErrMsgTxt("Command has timed out.\n");
		}
      sleep_count = 0;
      }
   }

#endif
}

char * GetString(char *szBuffer, int n, char *szSubString)
{
int i, j, k;
char szTest[5000];

szSubString[0] = '\0';
i = 0;
j = 0;
k = 0;
while (szBuffer[i] && (k <= n) )
	{
   szTest[j] = szBuffer[i];

   if (szBuffer[i] == '"')
   	{

      i++;
      j++;
      szTest[j] = szBuffer[i];

      /* we have a double quote; keep reading until EOF or another double quote */
      while(szBuffer[i] != '"' && szBuffer[i])
      	{
	      i++;
   	   j++;
		   szTest[j] = szBuffer[i];
         }
      }

   if (szTest[j] == ' ' || szTest[j] == '\n' || szTest[j] == '\r' || szTest[j] == '\0' || szTest[j] == ',')
   	{
      szTest[j] = '\0';
      if (k == n)
      	{
         strcpy(szSubString, szTest);
			return szSubString;
         }
      k++;
      j = -1;
      }
   i++;
   j++;
   }

szTest[j] = '\0';
if (k == n) strcpy(szSubString, szTest);

return szSubString;
}

int PostRequestMessage(char *szItem, char *szBuffer)
{
ATOM aItem;

aItem = GlobalAddAtom(szItem);

/* clear the buffers */
szGlobalBuffer[0] = '\0';
szBuffer[0] = '\0';

if (!PostMessage(hwndServer, WM_DDE_REQUEST, (WPARAM) hwndClient, PackDDElParam(WM_DDE_REQUEST, CF_TEXT, aItem)))
	{
   // MessageBox (hwndClient, "Cannot communicate with ZEMAX", "ZEMAX Inactive", MB_ICONEXCLAMATION | MB_OK);
   
   GlobalDeleteAtom(aItem);
   mexErrMsgTxt("Cannot communicate with ZEMAX. Please ensure ZEMAX is running.\n");
   }

WaitForData(hwndClient);
strcpy(szBuffer, szGlobalBuffer);

if (GotData) return 0;
else return -1;
}

int PostArrayTraceMessage(char *szBuffer, DDERAYDATA *RD)
{
ATOM aItem;
HGLOBAL hPokeData;
DDEPOKE * lpPokeData;
long numbytes;
int numrays;


if (RD[0].opd > 4)
	{
	/* NSC Rays */
	numrays = (int)RD[0].opd - 5;
	}
else
	{
	/* sequential rays */
	numrays = RD[0].error;
	}

/* point to where the data is */
rdpGRD = RD;
ngNumRays = numrays;

mexPrintf("numbytes = (1+numrays)*sizeof(DDERAYDATA);\n"); //debug
numbytes = (1+numrays)*sizeof(DDERAYDATA);

mexPrintf("hPokeData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (LONG) sizeof(DDEPOKE) + numbytes);\n"); //debug
hPokeData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (LONG) sizeof(DDEPOKE) + numbytes);

if (hPokeData == NULL) mexErrMsgTxt("Unable to allocate Global Memory for DDE.");

mexPrintf("lpPokeData = (DDEPOKE *) GlobalLock(hPokeData);\n"); //debug
lpPokeData = (DDEPOKE *) GlobalLock(hPokeData);

lpPokeData->fRelease = TRUE;

lpPokeData->cfFormat = CF_TEXT;

memcpy(lpPokeData->Value, RD, numbytes);

/* clear the buffers */
szGlobalBuffer[0] = '\0';
szBuffer[0] = '\0';

aItem = GlobalAddAtom("RayArrayData");
GlobalUnlock(hPokeData);

mexPrintf("Just Before the big DDE poke post.\n"); //debug

if (!PostMessage(hwndServer, WM_DDE_POKE, (WPARAM) hwndClient, PackDDElParam(WM_DDE_POKE, (UINT) hPokeData, aItem)))
	{
    // MessageBox (hwndClient, "Cannot communicate with ZEMAX!", "Hello?", MB_ICONEXCLAMATION | MB_OK);
    GlobalDeleteAtom(aItem);
    GlobalFree(hPokeData);
    mexErrMsgTxt("Cannot communicate with ZEMAX. Please ensure ZEMAX is running.\n");
	}
GlobalDeleteAtom(aItem);

mexPrintf("Just after the big poke.\n");

WaitForData(hwndClient);
strcpy(szBuffer, szGlobalBuffer);

/* clear the pointer */
rdpGRD = NULL;

if (GotData) return 0;
else return -1;
}

void MakeEmptyWindow(int text, char *szAppName, char *szOptions)
{
char szOutputFile[260], szModuleName[260], szBuffer[5000];
FILE *output;

/* get the output file name */
GetString(szCommandLine, 2, szOutputFile);

/* get the module name */
GetModuleFileName(globalhInstance, szModuleName, 255);

if ((output = fopen(szOutputFile, "wt")) == NULL)
	{
   /* can't open the file!! */
   return;
   }

if (text)
	{
   fputs("System is invalid, cannot compute data.\n",output);
   fclose(output);
	/* create a text window. Note we pass back the filename, module name, and activesurf as a single setting parameter. */
   sprintf(szBuffer,"MakeTextWindow,\"%s\",\"%s\",\"%s\",%s", szOutputFile, szModuleName, szAppName, szOptions);
   PostRequestMessage(szBuffer, szBuffer);
   }
else
	{
   fputs("NOFRAME\n",output);
   fputs("TEXT \"System is invalid, cannot compute data.\" .1 .5\n",output);
   fclose(output);
   sprintf(szBuffer,"MakeGraphicWindow,\"%s\",\"%s\",\"%s\",1,%s", szOutputFile, szModuleName, szAppName, szOptions);
   PostRequestMessage(szBuffer, szBuffer);
   }
}

void CenterWindow(HWND hwnd)
{
RECT rect;
int newx, newy;
GetWindowRect(hwnd, &rect);
newx = (GetSystemMetrics(SM_CXSCREEN) - (rect.right  - rect.left))/2;
newy = (GetSystemMetrics(SM_CYSCREEN) - (rect.bottom -  rect.top))/2;
SetWindowPos(hwnd, HWND_TOP, newx, newy, 0, 0, SWP_NOSIZE);
}

void Get_2_5_10(double cmax, double *cscale)
{
int i;
double temp;
if (cmax <= 0)
	{
	*cscale = .00001;
	return;
	}
*cscale = log10(cmax);
i = 0;
for (; *cscale < 0; i--) *cscale = *cscale + 1;
for (; *cscale > 1; i++) *cscale = *cscale - 1;
temp = 10;
if (*cscale < log10(5.0)) temp = 5;
if (*cscale < log10(2.0)) temp = 2;
*cscale = temp * pow(10, (double) i );
}

void remove_quotes(char *s)
{
int i=0;
/* remove the first quote if it exists */
if (s[0] == '"')
	{
	while (s[i])
		{
		s[i] = s[i+1];
		i++;
		}
	}
/* remove the last quote if it exists */
if (strlen(s) > 0)
	{
	if (s[strlen(s)-1] == '"') s[strlen(s)-1] = '\0';
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Here comes the UserFunction                                                                                  //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void UserFunction(void)
{

static char szBuffer[5000], szSub[256], szAppName[] = "Bulk Raytrace";

/* Now go and do the bulk raytracing */
mexPrintf("About to post ray data.\n"); //debug
PostArrayTraceMessage(szBuffer, MyRayData);

mexPrintf("Ray Data posted.\n"); //debug

}


// Returns an int from a Matlab mxArray numeric scalar
int myGetInt(mxArray *ScalarNumeric)
{
	int i;
	switch (mxGetClassID(ScalarNumeric))
		{
		case mxDOUBLE_CLASS : i = (int)*mxGetPr(ScalarNumeric);						return(i); break;
		case mxSINGLE_CLASS : i = (int)*(float *)mxGetData(ScalarNumeric);			return(i); break;
		case mxINT8_CLASS   : i = (int)*(char *)mxGetData(ScalarNumeric);			return(i); break;
		case mxUINT8_CLASS  : i = (int)*(unsigned char *)mxGetData(ScalarNumeric);	return(i); break;
		case mxINT16_CLASS  : i = (int)*(short *)mxGetData(ScalarNumeric);			return(i); break;
		case mxUINT16_CLASS : i = (int)*(unsigned short *)mxGetData(ScalarNumeric);	return(i); break;
		case mxINT32_CLASS  : i = *(int *)mxGetData(ScalarNumeric);					return(i); break;
		case mxUINT32_CLASS : i = (int)*(unsigned int *)mxGetData(ScalarNumeric);	return(i); break;
		}
	return(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -