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

📄 femmelua.cpp

📁 一个2D电磁场FEM计算的VC++源程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// femmeDoc.cpp : implementation of the CFemmeDoc class
//

#include "stdafx.h"
#include "femme.h"
#include "femmeDoc.h"
#include "femmeView.h"
#include "promptbox.h"
#include "MainFrm.h"

extern BOOL lua_byebye;

extern lua_State *lua;
extern BOOL bLinehook;
extern CLuaConsoleDlg *LuaConsole;
extern int m_luaWindowStatus;
extern CFemmeApp theApp;

CFemmeDoc *pFemmeDoc;

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

void CFemmeDoc::initalise_lua()
{
	// add lua error callback
//	lua_register(lua,"_ALERT",lua_ERROR); // use a messagebox to display errors

	// register lua extensions
    lua_register(lua,"savefemmfile",luaSaveDocument);
	lua_register(lua,"createmesh",lua_create_mesh);
	lua_register(lua,"showmesh",lua_show_mesh);
	lua_register(lua,"purgemesh",lua_purge_mesh);
	lua_register(lua,"probdef",lua_prob_def);
	lua_register(lua,"analyse",lua_analyze);
	lua_register(lua,"runpost",lua_runpost);
	lua_register(lua,"addnode",lua_addnode);
	lua_register(lua,"addblocklabel",lua_addlabel);
	lua_register(lua,"addsegment",lua_addline);
	lua_register(lua,"addarc",lua_addarc);
	lua_register(lua,"selectnode",lua_selectnode);
	lua_register(lua,"selectlabel",lua_selectlabel);
	lua_register(lua,"selectsegment",lua_selectsegment);
	lua_register(lua,"selectarcsegment",lua_selectarcsegment);
	lua_register(lua,"clearselected",lua_clearselected);
	lua_register(lua,"setnodeprop",lua_setnodeprop);
	lua_register(lua,"setblockprop",lua_setblockprop);
	lua_register(lua,"setsegmentprop",lua_setsegmentprop);
	lua_register(lua,"setarcsegmentprop",lua_setarcsegmentprop);
	lua_register(lua,"deleteselected",lua_deleteselected);
	lua_register(lua,"deleteselectednodes",lua_deleteselectednodes);
	lua_register(lua,"deleteselectedlabels",lua_deleteselectedlabels);
	lua_register(lua,"deleteselectedsegments",lua_deleteselectedsegments);
	lua_register(lua,"deleteselectedarcsegments",lua_deleteselectedarcsegments);
	lua_register(lua,"zoomnatural",lua_zoomnatural);
	lua_register(lua,"zoomout",lua_zoomout);
	lua_register(lua,"zoomin",lua_zoomin);
	lua_register(lua,"zoom",lua_zoom);
	lua_register(lua,"addmaterial",lua_addmatprop);
	lua_register(lua,"addpointprop",lua_addpointprop);
	lua_register(lua,"addcircprop",lua_addcircuitprop);
	lua_register(lua,"addboundprop",lua_addboundprop);
	lua_register(lua,"modifymaterial",lua_modmatprop);
	lua_register(lua,"modifyboundprop",lua_modboundprop);
	lua_register(lua,"modifypointprop",lua_modpointprop);
	lua_register(lua,"modifycircprop",lua_modcircprop);
	lua_register(lua,"deletematerial",lua_delmatprop);
	lua_register(lua,"deleteboundprop",lua_delboundprop);
	lua_register(lua,"deletecircuit",lua_delcircuitprop);
	lua_register(lua,"deletepointprop",lua_delpointprop);
	lua_register(lua,"moverotate",lua_move_rotate);
	lua_register(lua,"movetranslate",lua_move_translate);
	lua_register(lua,"copyrotate",lua_copy_rotate);
	lua_register(lua,"copytranslate",lua_copy_translate);
	lua_register(lua,"mirror",lua_mirror);
	lua_register(lua,"scale",lua_scale);
	lua_register(lua,"seteditmode",lua_seteditmode);
	lua_register(lua,"selectgroup",lua_selectgroup);
	lua_register(lua,"newdocument",lua_newdocument);
	lua_register(lua,"savebitmap",lua_savebitmap);
	lua_register(lua,"savemetafile",lua_saveWMF);
	lua_register(lua,"exitpre",lua_exitpre);	
	lua_register(lua,"addbhpoint",lua_addbhpoint);
	lua_register(lua,"clearbhpoints",lua_clearbhpoints);
	lua_register(lua,"refreshview",lua_updatewindow);
	lua_register(lua,"shownames",lua_shownames);
	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,"readdxf",lua_readdxf);
	lua_register(lua,"defineouterspace",lua_defineouterspace);
	lua_register(lua,"attachouterspace",lua_attachouterspace);
	lua_register(lua,"detachouterspace",lua_detachouterspace);
	lua_register(lua,"resize",luaResize);
	lua_register(lua,"minimize",luaMinimize);
	lua_register(lua,"maximize",luaMaximize);
	lua_register(lua,"restore", luaRestore);
	lua_register(lua,"createradius",lua_createradius);
    lua_register(lua,"openfemmfile",luaOpenDocument);
    
	// compatibility lua function names
	lua_register(lua,"open_femm_file",luaOpenDocument);
	lua_register(lua,"define_outer_space",lua_defineouterspace);
	lua_register(lua,"attach_outer_space",lua_attachouterspace);
	lua_register(lua,"detach_outer_space",lua_detachouterspace);
	lua_register(lua,"save_femm_file",luaSaveDocument);
	lua_register(lua,"create_mesh",lua_create_mesh);
	lua_register(lua,"show_mesh",lua_show_mesh);
	lua_register(lua,"purge_mesh",lua_purge_mesh);
	lua_register(lua,"prob_def",lua_prob_def);
	lua_register(lua,"analyze",lua_analyze);
	lua_register(lua,"run_post",lua_runpost);
	lua_register(lua,"add_node",lua_addnode);
	lua_register(lua,"add_block_label",lua_addlabel);
	lua_register(lua,"add_segment",lua_addline);
	lua_register(lua,"add_arc",lua_addarc);
	lua_register(lua,"select_node",lua_selectnode);
	lua_register(lua,"select_label",lua_selectlabel);
	lua_register(lua,"select_segment",lua_selectsegment);
	lua_register(lua,"select_arcsegment",lua_selectarcsegment);
	lua_register(lua,"clear_selected",lua_clearselected);
	lua_register(lua,"set_node_prop",lua_setnodeprop);
	lua_register(lua,"set_block_prop",lua_setblockprop);
	lua_register(lua,"set_segment_prop",lua_setsegmentprop);
	lua_register(lua,"set_arcsegment_prop",lua_setarcsegmentprop);
	lua_register(lua,"delete_selected",lua_deleteselected);
	lua_register(lua,"delete_selected_nodes",lua_deleteselectednodes);
	lua_register(lua,"delete_selected_labels",lua_deleteselectedlabels);
	lua_register(lua,"delete_selected_segments",lua_deleteselectedsegments);
	lua_register(lua,"delete_selected_arcsegments",lua_deleteselectedarcsegments);
	lua_register(lua,"zoom_natural",lua_zoomnatural);
	lua_register(lua,"zoom_out",lua_zoomout);
	lua_register(lua,"zoom_in",lua_zoomin);
	lua_register(lua,"add_material",lua_addmatprop);
	lua_register(lua,"add_point_prop",lua_addpointprop);
	lua_register(lua,"add_circ_prop",lua_addcircuitprop);
	lua_register(lua,"add_bound_prop",lua_addboundprop);
	lua_register(lua,"modify_material",lua_modmatprop);
	lua_register(lua,"modify_bound_prop",lua_modboundprop);
	lua_register(lua,"modify_point_prop",lua_modpointprop);
	lua_register(lua,"modify_circ_prop",lua_modcircprop);
	lua_register(lua,"delete_material",lua_delmatprop);
	lua_register(lua,"delete_bound_prop",lua_delboundprop);
	lua_register(lua,"delete_circuit",lua_delcircuitprop);
	lua_register(lua,"delete_point_prop",lua_delpointprop);
	lua_register(lua,"move_rotate",lua_move_rotate);
	lua_register(lua,"move_translate",lua_move_translate);
	lua_register(lua,"copy_rotate",lua_copy_rotate);
	lua_register(lua,"copy_translate",lua_copy_translate);
	lua_register(lua,"set_edit_mode",lua_seteditmode);
	lua_register(lua,"select_group",lua_selectgroup);
	lua_register(lua,"new_document",lua_newdocument);
	lua_register(lua,"save_bitmap",lua_savebitmap);
	lua_register(lua,"save_metafile",lua_saveWMF);
	lua_register(lua,"add_bh_point",lua_addbhpoint);
	lua_register(lua,"clear_bh_points",lua_clearbhpoints);
	lua_register(lua,"refresh_view",lua_updatewindow);
	lua_register(lua,"show_grid",lua_showgrid);
	lua_register(lua,"hide_grid",lua_hidegrid);
	lua_register(lua,"grid_snap",lua_gridsnap);
	lua_register(lua,"set_grid",lua_setgrid);
	lua_register(lua,"show_names",lua_shownames);
	lua_register(lua,"read_dxf",lua_readdxf);
	lua_register(lua,"exit_pre",lua_exitpre);	
	lua_register(lua,"create_radius",lua_createradius);

	lua_setlinehook(lua,line_hook);
	pFemmeDoc=this;

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

}

int CFemmeDoc::lua_exitpre(lua_State *L)
{
   lua_byebye=TRUE;

	return 0;
}

int CFemmeDoc::lua_readdxf(lua_State *L)
{
	
	CFemmeDoc * thisDoc;
	CFemmeView * theView;

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

	int n;
	CString DocTitle;
	double DefTol;
	BOOL result;

	// get title of desired document from Lua
	n=lua_gettop(L); if (n==0) return FALSE;
	DocTitle.Format("%s",lua_tostring(L,n));
	if (n==2) DefTol=lua_tonumber(L,1);
	else DefTol=0;

	result=thisDoc->ReadDXF(DocTitle,DefTol);
	if (result)	theView->InvalidateRect(NULL);

	return result;
}

int CFemmeDoc::luaOpenDocument(lua_State *L)
{
    CFemmeDoc * thisDoc;
    thisDoc=(CFemmeDoc *)pFemmeDoc;
    int n;
    n=lua_gettop(L);

    CString temp;
    temp.Format("%s",lua_tostring(L,n));
    if (thisDoc->OnOpenDocument(temp)!=FALSE)
        thisDoc->SetPathName(temp);

    return 0;
}

int CFemmeDoc::luaSaveDocument(lua_State *L)
{
	
	CFemmeDoc * thisDoc;
	thisDoc=(CFemmeDoc *)pFemmeDoc;
	int n;
	n=lua_gettop(L);

	CString temp;
	temp.Format("%s",lua_tostring(L,n));
	thisDoc->OnSaveDocument(temp);
	thisDoc->SetPathName(temp);

	return 0;
}

int CFemmeDoc::lua_prob_def(lua_State *L)
{
	
	CFemmeDoc * thisDoc;
	thisDoc=(CFemmeDoc *)pFemmeDoc;
	
	CString units,type;
	int n;
	n=lua_gettop(L);

	// error traps
	int LengthUnits=-1;
	int ProblemType=-1;
	double Frequency;
	double Precision;
	double MinAngle=-1;

	Frequency=fabs(lua_tonumber(L,1));
	units.Format("%s",lua_tostring(L,2));
	type.Format("%s",lua_tostring(L,3));
	Precision=lua_tonumber(L,4);
	if (n>4) thisDoc->Depth=lua_tonumber(L,5);
	if (n>5) MinAngle=lua_tonumber(L,6);
	if(units=="inches") LengthUnits=0;
	if(units=="millimeters") LengthUnits=1;
	if(units=="centimeters") LengthUnits=2;
	if(units=="meters") LengthUnits=3;
	if(units=="mills") LengthUnits=4;
	if(units=="mils") LengthUnits=4;
	if(units=="micrometers") LengthUnits=5;
	if(type=="planar") ProblemType=0;
	if(type=="axi") ProblemType=1;
	

	// error detection and nice variable handling

	if (LengthUnits==-1)
	{
		CString msg;
		msg.Format("Unknown length unit %s",units);
		lua_error(L,msg.GetBuffer(1));
		return 0;
	}
	else
	{
		thisDoc->LengthUnits=LengthUnits;
	}

	if (ProblemType==-1)
	{
		CString msg;
		msg.Format("Unknown problem type %s",type);
		lua_error(L,msg.GetBuffer(1));
		return 0;

	}
	else
	{
		thisDoc->ProblemType=ProblemType;
	}


	if (Frequency <0)
	{
		CString msg;
		msg.Format("Invalid Frequency %lf",Frequency);
		lua_error(L,msg.GetBuffer(1));
		return 0;

	}
	else
	{
		thisDoc->Frequency=Frequency;
	}

	if (Precision < 1.e-16 || Precision >1.e-8)
	{
		CString msg;
		msg.Format("Invalid Precision %lf",Precision);
		lua_error(L,msg.GetBuffer(1));
		return 0;

	}
	else
	{
		thisDoc->Precision=Precision;
	}

	if ((MinAngle>=1.) && (MinAngle<=33.8))
	{
		thisDoc->MinAngle=MinAngle;
	}

	return 0;
}

int CFemmeDoc::lua_create_mesh(lua_State *L)
{
	
	CFemmeDoc * thisDoc;
	CFemmeView * theView;

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

	theView->lnuMakeMesh();

	return 0;
}

int CFemmeDoc::lua_show_mesh(lua_State *L)
{
	
	CFemmeDoc * thisDoc;
	CFemmeView * theView;

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

	theView->lnu_show_mesh();

	return 0;
}

int CFemmeDoc::lua_purge_mesh(lua_State *L)
{
	
	CFemmeDoc * thisDoc;
	CFemmeView * theView;

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

	theView->lnu_purge_mesh();

	return 0;
}

int CFemmeDoc::lua_analyze(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>0) n=(int)lua_tonumber(L,1);
	if (n!=0) n=1;
	
	theView->lnu_analyze(n);

	return 0;
}

int CFemmeDoc::lua_runpost(lua_State *L)
{
        CString scriptfilename;
        scriptfilename.Format("%s",lua_tostring(L,1));

        // backward compatibility

        if (scriptfilename.Left(1)=="-")
        {
                scriptfilename=scriptfilename.Mid(1,scriptfilename.GetLength()-1);
        }


        CString vars,outparm;
		BOOL bHide=FALSE;
        int n;
        n=lua_gettop(L);
        vars="";
        if (n>1)
        {// we have vars as well
                for (int x=2 ;x<=n;x++)
                {
					outparm=lua_tostring(L,x);
					if((outparm.CompareNoCase("-windowhide")==0) ||
					   (outparm.CompareNoCase("-window_hide")==0))
					{
						bHide=TRUE;
						outparm="-windowhide";
					}
					vars=vars+" "+outparm;
                }
                scriptfilename+=vars; // append variable list
        }
        CFemmeDoc * thisDoc;
        CFemmeView * theView;

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

        theView->lnu_runpost(scriptfilename,bHide);

		return 0;
}


int CFemmeDoc::lua_addnode(lua_State *L)
{
	
	CFemmeDoc * thisDoc;
	CFemmeView * theView;
	double x,y,d;

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

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

	x=lua_tonumber(L,1);
	y=lua_tonumber(L,2);

//	if(thisDoc->Coords)
//	{
//		double r,t;
//		r=mx;
//		t=my*PI/180;
//		x=mx*cos(t);
//		y=mx*sin(t);
//	}
	
	if (thisDoc->nodelist.GetSize()<2) d=1.e-08;
	else{
		CComplex p0,p1,p2;
		p0=thisDoc->nodelist[0].CC();
		p1=p0;
		for(int i=1;i<thisDoc->nodelist.GetSize();i++)
		{
			p2=thisDoc->nodelist[i].CC();
			if(p2.re<p0.re) p0.re=p2.re;
			if(p2.re>p1.re) p1.re=p2.re;
			if(p2.im<p0.im) p0.im=p2.im;
			if(p2.im>p1.im) p1.im=p2.im;
		}
		d=abs(p1-p0)*1.e-06;
	}
	BOOL flag;
	flag=thisDoc->AddNode(x,y,d);
	
	if(flag==TRUE){
		theView->MeshUpToDate=FALSE;
		if(theView->MeshFlag==TRUE) theView->lnu_show_mesh();
		else theView->DrawPSLG();
	}

	return 0;
}

int CFemmeDoc::lua_addlabel(lua_State *L)
{
	
	CFemmeDoc * thisDoc;
	CFemmeView * theView;
	double x,y,d;

⌨️ 快捷键说明

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