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

📄 femmelua.cpp

📁 一个2D电磁场FEM计算的VC++源程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    // Create the .BMP file.
    hf = CreateFile(pszFile,
                   GENERIC_READ | GENERIC_WRITE,
                   (DWORD) 0,
                    NULL,
                   CREATE_ALWAYS,
                   FILE_ATTRIBUTE_NORMAL,
                   (HANDLE) NULL);
    if (hf == INVALID_HANDLE_VALUE)
    {//    errhandler("CreateFile", hwnd);
        AfxMessageBox("Critical error on getting bmp info, possible page fault ahoy20");

        }
        hdr.bfType = 0x4d42;        // 0x42 = "B" 0x4d = "M"
    // Compute the size of the entire file.
    hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) +
                 pbih->biSize + pbih->biClrUsed
                 * sizeof(RGBQUAD) + pbih->biSizeImage);
    hdr.bfReserved1 = 0;
    hdr.bfReserved2 = 0;

    // Compute the offset to the array of color indices.
    hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +
                    pbih->biSize + pbih->biClrUsed
                    * sizeof (RGBQUAD);

    // Copy the BITMAPFILEHEADER into the .BMP file.
    if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER),
        (LPDWORD) &dwTmp,  NULL))
    {
        AfxMessageBox("Critical error on getting bmp info, possible page fault ahoy21");

        //      errhandler("WriteFile", hwnd);
    }

    // Copy the BITMAPINFOHEADER and RGBQUAD array into the file.
    if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER)
                  + pbih->biClrUsed * sizeof (RGBQUAD),
                  (LPDWORD) &dwTmp, ( NULL)))
//        errhandler("WriteFile", hwnd);
        AfxMessageBox("Critical error on getting bmp info, possible page fault ahoy22");

    // Copy the array of color indices into the .BMP file.
    dwTotal = cb = pbih->biSizeImage;
    hp = lpBits;
    if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL))
//           errhandler("WriteFile", hwnd);
        AfxMessageBox("Critical error on getting bmp info, possible page fault ahoy23");

    // Close the .BMP file.
     if (!CloseHandle(hf))
//           errhandler("CloseHandle", hwnd);
        AfxMessageBox("Critical error on getting bmp info, possible page fault ahoy24");

    // Free memory.
    GlobalFree((HGLOBAL)lpBits);
}


int CFemmeDoc::lua_saveWMF(lua_State * L)
{
	
    CFemmeDoc * thisDoc;
    CFemmeView * theView;
    thisDoc=(CFemmeDoc *)pFemmeDoc;
    POSITION pos;
    pos=thisDoc->GetFirstViewPosition();
    theView=(CFemmeView *)thisDoc->GetNextView(pos);


    CString filename;

    filename.Format("%s",lua_tostring(L,1));

    RECT r;
    CDC tempDC;
    CDC *pDC=theView->GetDC();

    CMetaFileDC Meta;
    CRgn R;

    Meta.CreateEnhanced(NULL,NULL,NULL,NULL);
    theView->GetClientRect(&r);
    R.CreateRectRgnIndirect(&r);
    Meta.SelectClipRgn(&R);
    theView->OnDraw(&Meta);
    HENHMETAFILE hMeta=Meta.CloseEnhanced();

    DeleteEnhMetaFile(CopyEnhMetaFile(hMeta,filename));
	DeleteEnhMetaFile(hMeta);

	return 0;
}

int CFemmeDoc::lua_hidegrid(lua_State * L)
{
	
	CFemmeDoc * thisDoc;
	CFemmeView * theView;
	thisDoc=(CFemmeDoc *)pFemmeDoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmeView *)thisDoc->GetNextView(pos);

	CMainFrame *MFrm;
	MFrm=(CMainFrame *)theView->GetTopLevelFrame();
	CMenu* MMnu=MFrm->GetMenu();
	CToolBar *pToolBar;
	pToolBar=&MFrm->m_toolBar2;    
	CToolBarCtrl *tc=&pToolBar->GetToolBarCtrl();

	theView->GridFlag=FALSE;
	MMnu->CheckMenuItem(ID_SHOW_GRID, MF_UNCHECKED);
	tc->PressButton(ID_SHOW_GRID,FALSE);
	theView->InvalidateRect(NULL);

	return 0;
}


int CFemmeDoc::lua_showgrid(lua_State * L)
{
	
	CFemmeDoc * thisDoc;
	CFemmeView * theView;
	thisDoc=(CFemmeDoc *)pFemmeDoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmeView *)thisDoc->GetNextView(pos);

	CMainFrame *MFrm;
	MFrm=(CMainFrame *)theView->GetTopLevelFrame();
	CMenu* MMnu=MFrm->GetMenu();
	CToolBar *pToolBar;
	pToolBar=&MFrm->m_toolBar;    
	CToolBarCtrl *tc=&pToolBar->GetToolBarCtrl();

	theView->GridFlag=TRUE;
	MMnu->CheckMenuItem(ID_SHOW_GRID, MF_CHECKED);
	tc->PressButton(ID_SHOW_GRID,TRUE);
	theView->InvalidateRect(NULL);

	return 0;
}


int CFemmeDoc::lua_gridsnap(lua_State * L)
{
	
	CFemmeDoc * thisDoc;
	CFemmeView * theView;
	thisDoc=(CFemmeDoc *)pFemmeDoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmeView *)thisDoc->GetNextView(pos);

	CString temp;
	temp.Format("%s",lua_tostring(L,1));
	temp.MakeLower();

	CMainFrame *MFrm;
	MFrm=(CMainFrame *)theView->GetTopLevelFrame();
	CMenu* MMnu=MFrm->GetMenu();
	CToolBar *pToolBar;
	pToolBar=&MFrm->m_toolBar2;    
	CToolBarCtrl *tc=&pToolBar->GetToolBarCtrl();

	if(temp=="off"){
		theView->SnapFlag=FALSE;
		MMnu->CheckMenuItem(ID_SNAP_GRID, MF_UNCHECKED);
		tc->PressButton(ID_SNAP_GRID,FALSE);
	}
	else{
		theView->SnapFlag=TRUE;
		MMnu->CheckMenuItem(ID_SNAP_GRID, MF_CHECKED);
		tc->PressButton(ID_SNAP_GRID,TRUE);
	}
	

		if (temp !="on" && temp !="off")
	{
		CString msg="Unknown option for grid snap";
		lua_error(L,msg.GetBuffer(1));
		return 0;

	}


	return 0;
}


int CFemmeDoc::lua_setgrid(lua_State * L)
{
	
	CFemmeDoc * thisDoc;
	CFemmeView * theView;
	thisDoc=(CFemmeDoc *)pFemmeDoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmeView *)thisDoc->GetNextView(pos);

	CString temp;
	int coords=0;
	double gridsize;

	gridsize=lua_tonumber(L,1);
	temp.Format("%s",lua_tostring(L,2));
	temp.MakeLower();

	if (temp=="cart")  coords=0;
	if (temp=="polar") coords=1;

		if (temp !="cart" && temp !="polar")
	{
		CString msg="Unknown option for set grid";
		lua_error(L,msg.GetBuffer(1));
		return 0;

	}

	theView->GridSize=gridsize;
	thisDoc->Coords=coords;
	theView->InvalidateRect(NULL);

	return 0;
}

int CFemmeDoc::lua_shownames(lua_State * L)
{
	
	CFemmeDoc * thisDoc;
	CFemmeView * theView;
	thisDoc=(CFemmeDoc *)pFemmeDoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmeView *)thisDoc->GetNextView(pos);

	int flg=(int)lua_tonumber(L,1);
	if (flg!=0) flg=1;
	theView->ShowNames=flg;
	theView->InvalidateRect(NULL);

	return 0;
}

int CFemmeDoc::luaResize(lua_State *L)
{
/*
	
	CFemmeDoc * thisDoc;
	CFemmeView * theView;
	thisDoc=(CFemmeDoc *)pFemmeDoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmeView *)thisDoc->GetNextView(pos);
	CMainFrame *pMainFrm = (CMainFrame *)theView->GetParentFrame();
	int n=lua_gettop(L);
	if (n!=2) return 0;

	int nWidth  = (int)lua_tonumber(L,1);
	int nHeight = (int)lua_tonumber(L,2);
	pMainFrm->ResizeClient(nWidth,nHeight,TRUE);
*/
	return 0;
}

int CFemmeDoc::luaMinimize(lua_State *L)
{
	
	CFemmeDoc * thisDoc;
	CFemmeView * theView;
	thisDoc=(CFemmeDoc *)pFemmeDoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmeView *)thisDoc->GetNextView(pos);
	CMainFrame *pMainFrm = (CMainFrame *)theView->GetParentFrame();

	pMainFrm->ShowWindow(SW_MINIMIZE);

	return 0;
}

int CFemmeDoc::luaMaximize(lua_State *L)
{
	
	CFemmeDoc * thisDoc;
	CFemmeView * theView;
	thisDoc=(CFemmeDoc *)pFemmeDoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmeView *)thisDoc->GetNextView(pos);
	CMainFrame *pMainFrm = (CMainFrame *)theView->GetParentFrame();

	pMainFrm->ShowWindow(SW_MAXIMIZE);

	return 0;
}

int CFemmeDoc::luaRestore(lua_State *L)
{
	
	CFemmeDoc * thisDoc;
	CFemmeView * theView;
	thisDoc=(CFemmeDoc *)pFemmeDoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmeView *)thisDoc->GetNextView(pos);
	CMainFrame *pMainFrm = (CMainFrame *)theView->GetParentFrame();

	pMainFrm->ShowWindow(SW_RESTORE);

	return 0;
}

int CFemmeDoc::lua_defineouterspace(lua_State *L)
{
	
	CFemmeDoc * thisDoc;
	thisDoc=(CFemmeDoc *)pFemmeDoc;
	int n=lua_gettop(L);
	if (n!=3) return 0;

	thisDoc->extZo = fabs(lua_tonumber(L,1));
	thisDoc->extRo = fabs(lua_tonumber(L,2));
	thisDoc->extRi = fabs(lua_tonumber(L,3));

	if((thisDoc->extRo==0) || (thisDoc->extRi==0))
	{
		thisDoc->extZo = 0;
		thisDoc->extRo = 0;
		thisDoc->extRi = 0;
	}

	return 0;
}

int CFemmeDoc::lua_attachouterspace(lua_State *L)
{
	
	CFemmeDoc * thisDoc;
	thisDoc=(CFemmeDoc *)pFemmeDoc;

	for(int i=0;i<thisDoc->blocklist.GetSize();i++)
		if(thisDoc->blocklist[i].IsSelected==TRUE)
			thisDoc->blocklist[i].IsExternal=TRUE;
	
	return 0;
}

int CFemmeDoc::lua_detachouterspace(lua_State *L)
{
	
	CFemmeDoc * thisDoc;
	thisDoc=(CFemmeDoc *)pFemmeDoc;

	for(int i=0;i<thisDoc->blocklist.GetSize();i++)
		if(thisDoc->blocklist[i].IsSelected==TRUE)
			thisDoc->blocklist[i].IsExternal=FALSE;
	
	return 0;
}

int CFemmeDoc::lua_createradius(lua_State *L)
{
	
	CFemmeDoc * thisDoc;
	CFemmeView * theView;
	thisDoc=(CFemmeDoc *)pFemmeDoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmeView *)thisDoc->GetNextView(pos);

	int n=lua_gettop(L);
	if (n!=3) return 0;

	double x,y,r;
	int k;

	x = lua_tonumber(L,1);
	y = lua_tonumber(L,2);
	r = fabs(lua_tonumber(L,3));

	if ((k=thisDoc->ClosestNode(x,y))<0) return 0; // catch case where no nodes have been drawn yet;
	
	if (thisDoc->CanCreateRadius(k)==FALSE)
	{
		CString msg;
		msg.Format("The specified point is not suitable for conversion into a radius");
		lua_error(L,msg.GetBuffer(1));
		return 0;
	}

	if (thisDoc->CreateRadius(k,r))
	{
		theView->InvalidateRect(NULL);
		theView->MeshUpToDate=FALSE;
	}
	else{
		CString msg;
		msg.Format("Could not make a radius of the prescribed dimension");
		lua_error(L,msg.GetBuffer(1));
		return 0;
	}

	return 0;
}

void CFemmeView::lnu_runpost(CString scriptfilename,int bHide)
{
    CFemmeDoc *TheDoc = GetDocument();

    ASSERT_VALID(TheDoc);
    CString pn = TheDoc->GetPathName();
    if (pn.GetLength()==0){
        AfxMessageBox("No results to display",MB_OK | MB_ICONEXCLAMATION,0);
        return;
    }

    CString rootname= "\"" + pn.Left(pn.ReverseFind('.'))+".ans\"";
    char CommandLine[512];

    sprintf(CommandLine,"\"%sfemmview.exe\" %s -lua-script=%s",BinDir,rootname,scriptfilename);

    STARTUPINFO StartupInfo2 = {0};
    PROCESS_INFORMATION ProcessInfo2;
    StartupInfo2.cb = sizeof(STARTUPINFO);
    if (bHide==TRUE){
        StartupInfo2.dwFlags = STARTF_USESHOWWINDOW;
        StartupInfo2.wShowWindow = SW_MINIMIZE;
    }
    if (CreateProcess(NULL,CommandLine, NULL, NULL, FALSE,
            0, NULL, NULL, &StartupInfo2, &ProcessInfo2))
    {
        // block our code until return
        DWORD ExitCode;
        TheDoc->hProc=ProcessInfo2.hProcess;
        do{
            GetExitCodeProcess(ProcessInfo2.hProcess,&ExitCode);
            TheDoc->line_hook(lua,NULL);
        } while(ExitCode==STILL_ACTIVE);
        TheDoc->hProc=NULL;
    }
    else
    {
        AfxMessageBox("Problem executing the postprocessor");
        return;
    }
}

void CFemmeDoc::line_hook(lua_State *L, lua_Debug *ar)
{
    MSG msg;
	CFemmeDoc * thisDoc;
	thisDoc=(CFemmeDoc *)pFemmeDoc;

    while ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
    {
        if ((msg.message == WM_KEYDOWN) && (msg.wParam == VK_PAUSE))
        {
            bLinehook=FALSE;

            // If there is a running process (e.g. the solver), kill it
            if (thisDoc->hProc!=NULL) TerminateProcess(thisDoc->hProc,NULL);
            thisDoc->hProc=NULL;

            // bust out of script
            lua_error(L,NULL);
        }
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

int CFemmeDoc::lua_messagebox(lua_State *L)
{

    CString errmsg;
    errmsg.Format("%s",lua_tostring(L,1));


    CFemmeDoc * thisDoc;
    CFemmeView * theView;
    thisDoc=(CFemmeDoc *)pFemmeDoc;
    POSITION pos;
    pos=thisDoc->GetFirstViewPosition();
    theView=(CFemmeView *)thisDoc->GetNextView(pos);

    AfxMessageBox(errmsg,MB_ICONINFORMATION);

    return 0;
}

int CFemmeDoc::lua_afxpause(lua_State * L)
{
    AfxMessageBox("LUA pause");

    return 0;
}

int CFemmeDoc::lua_promptbox(lua_State *L)
{
    CPromptBox dlg;

    int n=lua_gettop(L);
    if(n>0) dlg.mytitle = lua_tostring(L,1);
    dlg.DoModal();
    lua_pushstring(L,dlg.instring);

    return TRUE;
}

⌨️ 快捷键说明

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