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

📄 femme.cpp

📁 一个2D电磁场FEM计算的VC++源程序
💻 CPP
字号:


// femme.cpp : Defines the class behaviors for the application.
//

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

#include "MainFrm.h"
#include "femmeDoc.h"
#include "femmeView.h"

#include "MyCommandLineInfo.h"
#include <afxctl.h>

extern "C" void lua_baselibopen (lua_State *L);
extern "C" void lua_iolibopen (lua_State *L);
extern "C" void lua_strlibopen (lua_State *L);
extern "C" void lua_mathlibopen (lua_State *L);
extern "C" void lua_dblibopen (lua_State *L);

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

extern int m_luaWindowStatus;

/////////////////////////////////////////////////////////////////////////////
// CFemmeApp

BEGIN_MESSAGE_MAP(CFemmeApp, CWinApp)
	//{{AFX_MSG_MAP(CFemmeApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFemmeApp construction

CFemmeApp::CFemmeApp()
{
	bActiveX=FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CFemmeApp object

CFemmeApp theApp;

// lua globals
lua_State * lua; // the main lua object
BOOL bLinehook;
BOOL lua_byebye=FALSE;
int m_luaWindowStatus=SW_SHOW;
CString luafiles[20]; 
int luafilepointer; // both used during the CMyCommandLineInfo parsing
CString luascriptname;
void *pFemmdoc;

/////////////////////////////////////////////////////////////////////////////
// CFemmeApp initialization

BOOL CFemmeApp::InitInstance()
{
    // Initialize OLE libraries
    if (!AfxOleInit())
    {
        AfxMessageBox("OLE initialization failed.  Make sure that the OLE libraries are the correct version.");
        return FALSE;
    }

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
//	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif
	SetRegistryKey("Gedanken Magnetics\\FEMM\\3");

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)
//	SetDialogBkColor();

	// Parse the command line to see if launched as OLE server
	if (RunEmbedded() || RunAutomated())
	{
		// Register all OLE server (factories) as running.  This enables the
		//  OLE libraries to create objects from other applications.
		COleTemplateServer::RegisterAll();
	}
	else
	{
		// When a server application is launched stand-alone, it is a good idea
		//  to update the system registry in case it has been damaged.
		COleObjectFactory::UpdateRegistryAll();
	}

	// {8B2BED07-A7DF-4DEB-8ECD-DFB1F0CC0FF5}
	const IID LIBID_ActiveFEMM = { 0x8B2BED07, 0xA7DF, 0x4DEB, { 0x8E, 0xCD, 0xDF, 0xB1, 0xF0, 0xCC, 0x0F, 0xF5 } };

	if(AfxOleRegisterTypeLib(AfxGetInstanceHandle(), LIBID_ActiveFEMM, _T("femme.TLB"))==FALSE)
		AfxMessageBox("TypeLib not registered!");

    // Initialize Lua
    bLinehook=FALSE;
    lua=lua_open(4096);
    lua_baselibopen(lua);
    lua_strlibopen(lua);
    lua_mathlibopen(lua);
    lua_iolibopen(lua);
	lua_register(lua,"_ALERT",lua_ERROR);

    // define some initial Lua variables
    lua_dostring(lua,"uo=PI*4.e-7");
    lua_dostring(lua,"eo=8.85418781762e-12");
    lua_dostring(lua,"function AWG(awg) return 8.252919195992115*exp(-0.11593801908089453*awg) end");
    lua_dostring(lua,"function IEC(iec) return 7.959159641581004*exp(-0.11519673572274754*iec) end");
    lua_dostring(lua,"Pi=PI; pi=PI");
    lua_dostring(lua,"meter=1");
    lua_dostring(lua,"meters=meter; Meter=meter; Meters=meter");
    lua_dostring(lua,"inch=0.0254");
    lua_dostring(lua,"inches=inch; Inch=inch; Inches=inch; in=inch;");
    lua_dostring(lua,"cm=0.01");
    lua_dostring(lua,"centimeter=cm; centimeters=cm; Centimeters=cm; Centimeter=cm");
    lua_dostring(lua,"mm=0.001");
    lua_dostring(lua,"millimeters=mm; millimeter=mm; Millimeter=mm; Millimeters=mm; milimeter=mm; milimeters=mm");
    lua_dostring(lua,"um=0.000001");
    lua_dostring(lua,"micrometer=um; Micrometer=um; micrometers=um; Micrometers=um");
    lua_dostring(lua,"micron=um; Micron=um; microns=um; Microns=um");
    lua_dostring(lua,"mil=0.001*inch");
    lua_dostring(lua,"mils=mil; Mils=mil; Mil=mil");

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CFemmeDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CFemmeView));
	AddDocTemplate(pDocTemplate);

	// Parse command line for standard shell commands, DDE, file open
	CMyCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);
	
	// lua extension
	// if we are running in quiet mode hide window
	this->m_nCmdShow=m_luaWindowStatus;

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
		// No message handlers
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CFemmeApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CFemmeApp commands

CString CFemmeApp::GetExecutablePath()
{
    char szPath[MAX_PATH];
    char szDrive[64];
    char szDir[MAX_PATH];

    GetModuleFileName(NULL, szPath, MAX_PATH);
    _splitpath(szPath, szDrive, szDir, NULL, NULL);

    return ((CString) szDrive) + szDir;
}

BOOL CFemmeApp::OnIdle(LONG lCount) 
{
	// if a lua script was specified on the command line, run it.
    // Invoking the lua script here, rather than in InitInstance,
    // makes sure that everything is initialized correctly before
    // the lua script starts to run.
    if (luascriptname.GetLength()!=0){
        if(m_luaWindowStatus==SW_SHOW) bLinehook=NormalLua;
        else bLinehook=HiddenLua;
        CString doscript=luascriptname;
        luascriptname.Empty();
        int lua_error_code;
        lua_byebye=FALSE;
        CStatusBar *StatBar=(CStatusBar *)((CMainFrame *)m_pMainWnd)->GetMessageBar();
        StatBar->SetPaneText(0,"EXECUTING LUASCRIPT -- HIT <BREAK> TO ABORT",TRUE);
        if (lua_error_code=lua_dofile(lua,doscript)!=0)
        {
            if (lua_error_code==LUA_ERRRUN)
                AfxMessageBox("Run Error");
            if (lua_error_code==LUA_ERRMEM)
                AfxMessageBox("Lua memory Error");
            if (lua_error_code==LUA_ERRERR)
                AfxMessageBox("User error error");
            if (lua_error_code==LUA_ERRFILE)
                AfxMessageBox("File Error");
        }
        StatBar->SetPaneText(0,"Ready",TRUE);
        if(lua_byebye==TRUE){
            ASSERT(AfxGetMainWnd() != NULL);
            AfxGetMainWnd()->PostMessage(WM_CLOSE);
        }
        bLinehook=FALSE;
    }

	return CWinApp::OnIdle(lCount);
}


int CFemmeApp::ExitInstance() 
{
	// TODO: Add your specialized code here and/or call the base class
	lua_close(lua);
	return CWinApp::ExitInstance();
}

int CFemmeApp::lua_ERROR(lua_State *L)
{

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

    // Somthing went wrong in lua execution
    // Windows dosn't have stdout so lets afxmessagebox it
/*
    if(theApp.bFileLink)
    {
        FILE* fp;
        fp=fopen(theApp.OFile,"wt");
        if(fp!=NULL){
            errmsg.Replace("error:","");
            errmsg.Replace("\"","\\\"");
            errmsg.Replace("\n","\\n");
            fprintf(fp,"error(\"FEMM returns:\\n%s\\n\")",errmsg);
            fclose(fp);
        }
    }
    else */
	if (!theApp.bActiveX) AfxMessageBox(errmsg);

    return 0;
}

⌨️ 快捷键说明

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