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

📄 femmviewlua.cpp

📁 一个2D电磁场FEM计算的VC++源程序
💻 CPP
📖 第 1 页 / 共 4 页
字号:
#include "stdafx.h"
#include <afx.h>
#include <afxtempl.h>
#include "problem.h"
#include "femmview.h"
#include "xyplot.h"
#include "ptloc.h"
#include "femmviewDoc.h"
#include "femmviewview.h"
#include "MainFrm.h"
#include "windows.h" //necessary for bitmap saving
#include "lua.h"
#include "mycommandlineinfo.h"
#include "promptbox.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern lua_State *lua;
extern BOOL bLinehook;
CFemmviewDoc *pFemmviewdoc;
extern CLuaConsoleDlg *LuaConsole;
extern BOOL lua_byebye;
extern int m_luaWindowStatus;


//-------------------Lua Extensions-------------------------

void CFemmviewDoc::initalise_lua()
{
	// use a messagebox to display errors
	lua_register(lua,"messagebox",lua_messagebox);
	lua_register(lua,"getpointvalues",lua_getpointvals);
	lua_register(lua,"exitpost",lua_exitpost);
	lua_register(lua,"addcontour",lua_addcontour);
	lua_register(lua,"clearcontour",lua_clearcontour);
	lua_register(lua,"lineintegral",lua_lineintergral);
	lua_register(lua,"selectblock",lua_selectblock);
	lua_register(lua,"groupselectblock",lua_groupselectblock);
	lua_register(lua,"clearblock",lua_clearblock);
	lua_register(lua,"blockintegral",lua_blockintergral);
	lua_register(lua,"zoomnatural",lua_zoomnatural);
	lua_register(lua,"zoomin",lua_zoomin);
	lua_register(lua,"zoomout",lua_zoomout);
	lua_register(lua,"zoom",lua_zoom);
	lua_register(lua,"showgrid",lua_showgrid);
	lua_register(lua,"hidegrid",lua_hidegrid);
	lua_register(lua,"gridsnap",lua_gridsnap);
	lua_register(lua,"setgrid",lua_setgrid);
	lua_register(lua,"showmesh",lua_showmesh);
	lua_register(lua,"hidemesh",lua_hidemesh);
	lua_register(lua,"hidedensityplot",lua_hidedensity);
	lua_register(lua,"showdensityplot",lua_showdensity);
	lua_register(lua,"hidecontourplot",lua_hidecountour);
	lua_register(lua,"showcontourplot",lua_showcountour);
	lua_register(lua,"showvectorplot",lua_vectorplot);
	lua_register(lua,"smooth",lua_smoothing);
	lua_register(lua,"showpoints",lua_showpoints);
	lua_register(lua,"hidepoints",lua_hidepoints);
	lua_register(lua,"shownames",lua_shownames);
	lua_register(lua,"getprobleminfo",lua_getprobleminfo);
	lua_register(lua,"savebitmap",lua_savebitmap);
	lua_register(lua,"getcircuitproperties",lua_getcircuitprops);
	lua_register(lua,"savemetafile",lua_saveWMF);
    lua_register(lua,"refreshview",lua_refreshview);
	lua_register(lua,"selectpoint",lua_selectline);
	lua_register(lua,"seteditmode",lua_seteditmode);
	lua_register(lua,"bendcontour",lua_bendcontour);
	lua_register(lua,"makeplot",lua_makeplot);
	lua_register(lua,"reload",lua_reload);
	lua_register(lua,"gradient",lua_gradient);
	lua_register(lua,"showpointprops",lua_showpointprops);
	lua_register(lua,"hidepointprops",lua_showpointprops);

	lua_register(lua,"minimize",luaMinimize);
	lua_register(lua,"maximize",luaMaximize);
	lua_register(lua,"restore", luaRestore);

	// compatibility with alternate lua function naming convention
	lua_register(lua,"message_box",lua_messagebox);
	lua_register(lua,"show_point_props",lua_showpointprops);
	lua_register(lua,"hide_point_props",lua_showpointprops);
	lua_register(lua,"exit_post",lua_exitpost);
	lua_register(lua,"get_point_values",lua_getpointvals);
	lua_register(lua,"add_contour",lua_addcontour);
	lua_register(lua,"clear_contour",lua_clearcontour);
	lua_register(lua,"line_integral",lua_lineintergral);
	lua_register(lua,"select_block",lua_selectblock);
	lua_register(lua,"group_select_block",lua_groupselectblock);
	lua_register(lua,"clear_block",lua_clearblock);
	lua_register(lua,"block_integral",lua_blockintergral);
	lua_register(lua,"zoom_natural",lua_zoomnatural);
	lua_register(lua,"zoom_in",lua_zoomin);
	lua_register(lua,"zoom_out",lua_zoomout);
	lua_register(lua,"show_grid",lua_showgrid);
	lua_register(lua,"hide_grid",lua_hidegrid);
	lua_register(lua,"show_mesh",lua_showmesh);
	lua_register(lua,"hide_mesh",lua_hidemesh);
	lua_register(lua,"hide_density_plot",lua_hidedensity);
	lua_register(lua,"show_density_plot",lua_showdensity);
	lua_register(lua,"hide_contour_plot",lua_hidecountour);
	lua_register(lua,"show_contour_plot",lua_showcountour);
	lua_register(lua,"show_vector_plot",lua_vectorplot);
	lua_register(lua,"show_points",lua_showpoints);
	lua_register(lua,"hide_points",lua_hidepoints);
	lua_register(lua,"grid_snap",lua_gridsnap);
	lua_register(lua,"set_grid",lua_setgrid);
	lua_register(lua,"get_problem_info",lua_getprobleminfo);
	lua_register(lua,"save_bitmap",lua_savebitmap);
	lua_register(lua,"get_circuit_properties",lua_getcircuitprops);
	lua_register(lua,"save_metafile",lua_saveWMF);
    lua_register(lua,"refresh_view",lua_refreshview);
	lua_register(lua,"select_point",lua_selectline);
	lua_register(lua,"set_edit_mode",lua_seteditmode);
	lua_register(lua,"bend_contour",lua_bendcontour);
	lua_register(lua,"make_plot",lua_makeplot);
	lua_register(lua,"show_names",lua_shownames);

 	pFemmviewdoc=this;

	LuaConsole.Create(IDD_LUACONSOLE);
    if ((d_luaconsole!=FALSE) && (m_luaWindowStatus!=SW_MINIMIZE))
        LuaConsole.ShowWindow(SW_SHOW);

}

int CFemmviewDoc::lua_dumpheader(lua_State * L)
{
	
	CFemmviewDoc * thisDoc;
	CFemmviewView * theView;

	thisDoc=(CFemmviewDoc *)pFemmviewdoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmviewView *)thisDoc->GetNextView(pos);

	return 0;
}

int CFemmviewDoc::lua_getpointvals(lua_State * L)
{
	
	double px,py;

	px=lua_tonumber(L,1);
	py=lua_tonumber(L,2);

	CFemmviewDoc * thisDoc;
	CFemmviewView * theView;

	thisDoc=(CFemmviewDoc *)pFemmviewdoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();

	theView=(CFemmviewView *)thisDoc->GetNextView(pos);

	CPointVals u;
	CComplex Jtot;

	if(thisDoc->GetPointValues(px, py, u)==TRUE)
	{
		lua_pushnumber(L,u.A.re);
		lua_pushnumber(L,u.A.im);
		lua_pushnumber(L,u.B1.re);
		lua_pushnumber(L,u.B1.im);
		lua_pushnumber(L,u.B2.re);
		lua_pushnumber(L,u.B2.im);
		lua_pushnumber(L,u.c);
		lua_pushnumber(L,u.E);
		lua_pushnumber(L,u.H1.re);
		lua_pushnumber(L,u.H1.im);
		lua_pushnumber(L,u.H2.re);
		lua_pushnumber(L,u.H2.im);
		lua_pushnumber(L,u.Je.re);
		lua_pushnumber(L,u.Je.im);
		lua_pushnumber(L,u.Js.re);
		lua_pushnumber(L,u.Js.im);
		lua_pushnumber(L,u.mu1.re);
		lua_pushnumber(L,u.mu1.im);
		lua_pushnumber(L,u.mu2.re);
		lua_pushnumber(L,u.mu2.im);
		lua_pushnumber(L,u.Pe);
		lua_pushnumber(L,u.Ph);
		lua_pushnumber(L,u.ff);
		return 23;
	}

	return 0;
}

int CFemmviewDoc::lua_exitpost(lua_State * L)
{
	lua_byebye=TRUE;
    return 0;
}

int CFemmviewDoc::lua_reload(lua_State * L)
{
	

	CFemmviewDoc * thisDoc;
	CFemmviewView * theView;
	thisDoc=(CFemmviewDoc *)pFemmviewdoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmviewView *)thisDoc->GetNextView(pos);

	theView->BeginWaitCursor();
	thisDoc->OnReload();
	theView->EndWaitCursor();
	if (theView->d_ResetOnReload==FALSE) thisDoc->FirstDraw=FALSE;
	theView->RedrawView();
	theView->LuaViewInfo();
	return 0;
}

void CFemmviewView::LuaViewInfo()
{
	OnViewInfo();
}

int CFemmviewDoc::lua_addcontour(lua_State * L)
{
	
	CComplex z;
	int i;
	z.Set(lua_tonumber(L,1),lua_tonumber(L,2));
	
	CFemmviewDoc * thisDoc;
	CFemmviewView * theView;
	thisDoc=(CFemmviewDoc *)pFemmviewdoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmviewView *)thisDoc->GetNextView(pos);

	i=thisDoc->contour.GetSize();

	if(i>0){
		if (z!=thisDoc->contour[i-1])
				thisDoc->contour.Add(z);
	}
	else thisDoc->contour.Add(z);
	theView->DrawUserContour(FALSE);
	
	return 0;
}

int CFemmviewDoc::lua_clearcontour(lua_State * L)
{
	
	CFemmviewDoc * thisDoc;
	CFemmviewView * theView;
	thisDoc=(CFemmviewDoc *)pFemmviewdoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmviewView *)thisDoc->GetNextView(pos);

	theView->EraseUserContour(TRUE);
	thisDoc->contour.RemoveAll();
	
	return 0;
}

int CFemmviewDoc::lua_lineintergral(lua_State * L)
{
	
	int type;
	CComplex *z;
	z=(CComplex *)calloc(4,sizeof(CComplex));

	type=(int) lua_tonumber(L,1); 
	// 0- B.n
	// 1 - H.t
	// 2 - Cont length
	// 3 - Force from stress tensor
	// 4 - Torque from stress tensor
	// 5 - (B.n)^2

	if (type<0 || type >5)
	{
		CString msg;
		msg.Format("Invalid line integral selected %d",type);
		lua_error(L,msg.GetBuffer(1));
		return 0;

	}

	CFemmviewDoc * thisDoc;
	CFemmviewView * theView;
	thisDoc=(CFemmviewDoc *)pFemmviewdoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmviewView *)thisDoc->GetNextView(pos);

	thisDoc->LineIntegral(type,z);

	switch(type)
	{
		case 3: // force results
			if (thisDoc->Frequency!=0){
				lua_pushnumber(lua,z[2].re);
				lua_pushnumber(lua,0);
				lua_pushnumber(lua,z[3].re);
				lua_pushnumber(lua,0);
				lua_pushnumber(lua,z[0].re);
				lua_pushnumber(lua,z[0].im);
				lua_pushnumber(lua,z[1].re);
				lua_pushnumber(lua,z[1].im);
				free(z);
				return 8;
			}
			else{
				lua_pushnumber(lua,z[0].re);
				lua_pushnumber(lua,0);
				lua_pushnumber(lua,z[1].re);
				lua_pushnumber(lua,0);
				free(z);
				return 4;
			}


		case 4: // torque results
			if(thisDoc->Frequency!=0){
				lua_pushnumber(lua,z[1].re);
				lua_pushnumber(lua,0);
				lua_pushnumber(lua,z[0].re);
				lua_pushnumber(lua,z[0].im);
				free(z);
				return 4;
			}
			else
			{
				lua_pushnumber(lua,z[0].re);
				lua_pushnumber(lua,0);
				lua_pushnumber(lua,0);
				lua_pushnumber(lua,0);
		
				free(z);
				return 4;
			}

		default:
			lua_pushnumber(lua,z[0].re);
			lua_pushnumber(lua,z[0].im);

			lua_pushnumber(lua,z[1].re);
			lua_pushnumber(lua,z[1].im);
	
			free(z);
			return 4;
	}

	free(z);
	return 0;
}

int CFemmviewDoc::lua_selectblock(lua_State * L)
{
	
	double px,py;

	px=lua_tonumber(L,1);
	py=lua_tonumber(L,2);

	CFemmviewDoc * thisDoc;
	CFemmviewView * theView;
	thisDoc=(CFemmviewDoc *)pFemmviewdoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmviewView *)thisDoc->GetNextView(pos);
	theView->EditAction=2;
	int k;
	
	if (thisDoc->meshelem.GetSize()>0){
		k=thisDoc->InTriangle(px,py);
		if(k>=0){
			thisDoc->bHasMask=FALSE;
			thisDoc->blocklist[thisDoc->meshelem[k].lbl].ToggleSelect();
			//	RedrawView();
			theView->DrawSelected=thisDoc->meshelem[k].lbl;
			HDC myDCH=GetDC(theView->m_hWnd);
			CDC tempDC;
			CDC *pDC;
			pDC=tempDC.FromHandle(myDCH);
				
			//CDC *pDC=GetDC(theView->m_hWnd);
			theView->OnDraw(pDC);
			theView->DrawSelected=-1;
			theView->ReleaseDC(pDC);
			thisDoc->UpdateAllViews(theView);
		}
	}

	return 0;
}

int CFemmviewDoc::lua_groupselectblock(lua_State * L)
{
	
	CFemmviewDoc * thisDoc;
	CFemmviewView * theView;
	thisDoc=(CFemmviewDoc *)pFemmviewdoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmviewView *)thisDoc->GetNextView(pos);
	theView->EditAction=2;
	int j,k,n;
	
	if (thisDoc->meshelem.GetSize()>0)
	{
		n=lua_gettop(L);
		k=0; if (n>0) k=(int)lua_tonumber(L,1);

		for(j=0;j<thisDoc->blocklist.GetSize();j++)
		{
			if ((thisDoc->blocklist[j].InGroup==k) || (n==0))
			thisDoc->blocklist[j].ToggleSelect();
			thisDoc->bHasMask=FALSE;
		}

		HDC myDCH=GetDC(theView->m_hWnd);
		CDC tempDC;
		CDC *pDC;
		pDC=tempDC.FromHandle(myDCH);
				
		//CDC *pDC=GetDC(theView->m_hWnd);
		theView->OnDraw(pDC);
		theView->DrawSelected=-1;
		theView->ReleaseDC(pDC);
		thisDoc->UpdateAllViews(theView);
	}

	return 0;
}

int CFemmviewDoc::lua_clearblock(lua_State * L)
{
	
	CFemmviewDoc * thisDoc;
	CFemmviewView * theView;
	thisDoc=(CFemmviewDoc *)pFemmviewdoc;
	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmviewView *)thisDoc->GetNextView(pos);

	thisDoc->bHasMask=FALSE;
	for(int i=0;i<thisDoc->blocklist.GetSize();i++)
	{
		if (thisDoc->blocklist[i].IsSelected==TRUE)
			thisDoc->blocklist[i].IsSelected=FALSE;
	}
	
	
	theView->EditAction=0;
	
	theView->RedrawView();

	//	HDC myDCH=GetDC(theView->m_hWnd);
	//	CDC tempDC;
	//		CDC *pDC;
	//	pDC=tempDC.FromHandle(myDCH);
				
	//CDC *pDC=GetDC(theView->m_hWnd);
	//	theView->OnDraw(pDC);
	//	theView->DrawSelected=-1;
	//	theView->ReleaseDC(pDC);
	//	thisDoc->UpdateAllViews(theView);

	return 0;
}


int CFemmviewDoc::lua_blockintergral(lua_State * L)
{
	
	int type;
	type=(int) lua_tonumber(L,1);

	CFemmviewDoc *thisDoc;
	CFemmviewView * theView;
	thisDoc=(CFemmviewDoc *)pFemmviewdoc;
	

	POSITION pos;
	pos=thisDoc->GetFirstViewPosition();
	theView=(CFemmviewView *)thisDoc->GetNextView(pos);

	CComplex z;

	int i;
	BOOL flg=FALSE;
	for(i=0;i<thisDoc->blocklist.GetSize();i++)
		if (thisDoc->blocklist[i].IsSelected==TRUE) flg=TRUE;
	if(flg==FALSE)
	{
		CString msg="Cannot integrate\nNo area has been selected";
		lua_error(L,msg.GetBuffer(1));
		return 0;
	}
	if((type>=18) && (type<=23)) thisDoc->MakeMask();
	z=thisDoc->BlockIntegral(type); 

	lua_pushnumber(L,z.re);

⌨️ 快捷键说明

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