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

📄 activefemm.cpp

📁 一个2D电磁场FEM计算的VC++源程序
💻 CPP
字号:
// ActiveFEMM.cpp : implementation file
//

#include "stdafx.h"
#include "lua.h"
#include "luadebug.h"
#include "femme.h"

#include "luaconsoledlg.h"
#include "ActiveFEMM.h"

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

extern lua_State *lua;
extern BOOL bLinehook;
extern BOOL lua_byebye;
//extern HANDLE hProc;
extern int m_luaWindowStatus;
extern CFemmeApp theApp;

CString LuaResult;

/////////////////////////////////////////////////////////////////////////////
// ActiveFEMM

IMPLEMENT_DYNCREATE(ActiveFEMM, CCmdTarget)

ActiveFEMM::ActiveFEMM()
{
	EnableAutomation();
	EnableTypeLib();

	// To keep the application running as long as an OLE automation
	//  object is active, the constructor calls AfxOleLockApp.

	AfxOleLockApp();
	lua_register(lua,"actxprint", lua_to_string);
	lua_register(lua,"lua2matlab",lua_to_matlab);
}

ActiveFEMM::~ActiveFEMM()
{
	// To terminate the application when all objects created with
	//  with OLE automation, the destructor calls AfxOleUnlockApp.

	AfxOleUnlockApp();
}


void ActiveFEMM::OnFinalRelease()
{
    // When the last reference for an automation object is released
    // OnFinalRelease is called.  The base class will automatically
    // deletes the object.  Add additional cleanup required for your
    // object before calling the base class.

    // We have to close things in a funny way so that FEMM shuts down
    // the way that it expects to. First, the call to AfxOleSetUserCtrl
    // makes it so that the the application won't get closed when the
    // base class version of OnFinalRelease gets called.
    AfxOleSetUserCtrl(TRUE);

    // Then, post a message to the main window requesting a shutdown.
    // This is the way that FEMM likes to shut down. Since the message
    // has been posted rather than sent, it will be acted upon after
    // ActiveFEMM has shut itself down.
    AfxGetMainWnd()->PostMessage(WM_CLOSE);

	CCmdTarget::OnFinalRelease();
}


BEGIN_MESSAGE_MAP(ActiveFEMM, CCmdTarget)
	//{{AFX_MSG_MAP(ActiveFEMM)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(ActiveFEMM, CCmdTarget)
	//{{AFX_DISPATCH_MAP(ActiveFEMM)
	DISP_FUNCTION(ActiveFEMM, "call2femm", call2femm, VT_BSTR, VTS_BSTR)
	DISP_FUNCTION(ActiveFEMM, "mlab2femm", mlab2femm, VT_BSTR, VTS_BSTR)
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()


/////////////////////////////////////////////////////////////////////////////
// Type library ID and version
// {8B2BED07-A7DF-4DEB-8ECD-DFB1F0CC0FF5}
static const GUID _tlid = 
{ 0x8B2BED07, 0xA7DF, 0x4DEB, { 0x8E, 0xCD, 0xDF, 0xB1, 0xF0, 0xCC, 0x0F, 0xF5 } };
const WORD _wVerMajor = 1;
const WORD _wVerMinor = 0;
IMPLEMENT_OLETYPELIB(ActiveFEMM, _tlid, _wVerMajor, _wVerMinor)


// Note: we add support for IID_IActiveFEMM to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the 
//  dispinterface in the .ODL file.

// {CED86EB6-DE64-40F6-AFBF-DA8399526C20}
static const IID IID_IActiveFEMM =
{ 0xced86eb6, 0xde64, 0x40f6, { 0xaf, 0xbf, 0xda, 0x83, 0x99, 0x52, 0x6c, 0x20 } };

BEGIN_INTERFACE_MAP(ActiveFEMM, CCmdTarget)
	INTERFACE_PART(ActiveFEMM, IID_IActiveFEMM, Dispatch)
END_INTERFACE_MAP()

// {3D1E0DFB-802B-4098-8614-07AB0CCFF2F2}
IMPLEMENT_OLECREATE(ActiveFEMM, "femme.ActiveFEMM", 0x3d1e0dfb, 0x802b, 0x4098, 0x86, 0x14, 0x07, 0xab, 0x0c, 0xcf, 0xf2, 0xf2)

BOOL ActiveFEMM::GetDispatchIID(IID* pIID)
{
         *pIID = IID_IActiveFEMM;

         return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// ActiveFEMM message handlers

BSTR ActiveFEMM::call2femm(LPCTSTR luacmd) 
{
	// executes the line contained in luacmd
	// and returns a string containing the results
	// of the command with the results separated
	// by newline characters.
	CString strToLua;

	strToLua=luacmd;
	strToLua="actxprint(" + strToLua +")";
	DoLuaCmd(strToLua);

	return LuaResult.AllocSysString();
}

BSTR ActiveFEMM::mlab2femm(LPCTSTR luacmd) 
{
	// executes the line contained in luacmd
	// and returns a string containing the results
	// of the command formatted in matlab format.
	// One would expect that all the results are
	// real numbers, in which case we can eval()
	// the result in matlab to get a vector of numbers.
	CString strToLua;

	strToLua=luacmd;
	strToLua="lua2matlab(" + strToLua +")";
	DoLuaCmd(strToLua);

	return LuaResult.AllocSysString();
}

void ActiveFEMM::DoLuaCmd(CString strToLua)
{
	LuaResult.Empty();
	if(m_luaWindowStatus==SW_SHOW) bLinehook=1;
	else bLinehook=2;
	
	theApp.bActiveX=TRUE;
	if (lua_dostring(lua,strToLua)!=0) LuaResult=theApp.LuaErrmsg;
	theApp.bActiveX=FALSE;
	lua_byebye=FALSE;
	bLinehook=FALSE;
}

int ActiveFEMM::lua_to_string(lua_State *L)
{
	CString s;

	int n = lua_gettop(L);	
	LuaResult="";

	for(int k=1;k<=n;k++)
	{
		s=lua_tostring(L,k);
		LuaResult = LuaResult + s +"\n";
	}
	
	return 0;
}

int ActiveFEMM::lua_to_matlab(lua_State *L)
{
	CString s;

	int n = lua_gettop(L);	
	if(n>0){
		LuaResult="[ ";
		for(int k=1;k<=n;k++)
		{
			s=lua_tostring(L,k);
			LuaResult = LuaResult + s + " ";
		}
		LuaResult += "]";
	}
	else LuaResult.Empty();

	return 0;
}

⌨️ 快捷键说明

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