www.pudn.com > p333333.rar > SimulinkVCDlg.cpp


// SimulinkVCDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "SimulinkVC.h" 
#include "SimulinkVCDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
	 
#include "HyperLink.h" 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	CHyperLink	m_email; 
	//}}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) 
	virtual BOOL OnInitDialog(); 
	//}}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) 
	DDX_Control(pDX, IDC_MAILTOME, m_email); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CSimulinkVCDlg dialog 
 
CSimulinkVCDlg::CSimulinkVCDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CSimulinkVCDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CSimulinkVCDlg) 
	m_fStartTime = 0.0f; 
	m_fStopTime = 0.023f; 
	m_dangle = 30.0; 
	m_dw = 500.0; 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CSimulinkVCDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CSimulinkVCDlg) 
	DDX_Control(pDX, IDC_BTN_START, m_btnStart); 
	DDX_Control(pDX, IDC_COMBO_SOLVER, m_cmbSolver); 
	DDX_Text(pDX, IDC_EDIT_STARTTIME, m_fStartTime); 
	DDX_Text(pDX, IDC_EDIT_STOPTIME, m_fStopTime); 
	DDX_Text(pDX, IDC_EDIT_ANGLE, m_dangle); 
	DDX_Text(pDX, IDC_EDIT_W, m_dw); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CSimulinkVCDlg, CDialog) 
	//{{AFX_MSG_MAP(CSimulinkVCDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BTN_START, OnBtnStart) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CSimulinkVCDlg message handlers 
 
BOOL CSimulinkVCDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization here 
	m_cmbSolver.SetCurSel(7); 
	m_bRun = FALSE; 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CSimulinkVCDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CSimulinkVCDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
		 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
		 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
		 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CSimulinkVCDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CSimulinkVCDlg::OnBtnStart()  
{ 
	// TODO: Add your control notification handler code here 
	if (!m_bRun) 
	{ 
		UpdateData(TRUE); 
		 
		// 打开引擎 
		 
		if (!(ep = engOpen(NULL)))  
		{ 
			MessageBox("无法打开MATLAB引擎",  MB_OK); 
		} 
		 
		m_bRun = TRUE; 
		m_btnStart.SetWindowText("停止仿真"); 
		 
		// 仿真时间和正弦波参数的设定,注意T的用法 
		mxArray *T = NULL;		// matlab中用的仿真时间 
		mxArray *W = NULL;		// 正弦波角频率(rad/sec) 
		mxArray *A = NULL;		// 正弦波起始相位角(rad) 
		 
		double timespan[2]; 
		timespan[0] = (double) m_fStartTime; 
		timespan[1] = (double) m_fStopTime; 
		T = mxCreateDoubleMatrix(1, 2, mxREAL); 
		memcpy((char *) mxGetPr(T), (char *) timespan, 2*sizeof(double)); 
		engPutVariable(ep, "T", T); 
		 
		// 将正弦波参数放入matlab工作区已备仿真调用 
		W = mxCreateDoubleMatrix(1, 1, mxREAL); 
		memcpy((char *) mxGetPr(W), (char *) &m_dw, sizeof(double)); 
		engPutVariable(ep, "W", W); 
		 
		A = mxCreateDoubleMatrix(1, 1, mxREAL); 
		memcpy((char *) mxGetPr(A), (char *) &m_dangle, sizeof(double));	 
		engPutVariable(ep, "A", A); 
		 
		// 仿真Solver 
		/****************************************************/ 
		/*  0 ----------- discrete(no contonuous states)	*/ 
		/*	1 ----------- ode45(Dormand Prince)				*/ 
		/*	2 ----------- ode23(Bogacki-Shampine)			*/ 
		/*	3 ----------- ode113(Adams)						*/ 
		/*	4 ----------- ode15s(stiff/NDF)					*/ 
		/*	5 ----------- ode23s(stiff/Mod.Rosenbrock)		*/ 
		/*	6 ----------- ode23t(Mod.stiff/Trapezoidal)		*/ 
		/*	7 ----------- ode23tb(stiff/TR_BDF2)			*/ 
		/****************************************************/ 
		 
		int nSolver; 
		 
		nSolver = m_cmbSolver.GetCurSel(); 
		switch (nSolver) 
		{ 
		case 0: 
			engEvalString(ep, "mySolver = 'discrete';");	 
			break; 
		case 1: 
			engEvalString(ep, "mySolver = 'ode45';"); 
			break; 
		case 2: 
			engEvalString(ep, "mySolver = 'ode23';"); 
			break; 
		case 3: 
			engEvalString(ep, "mySolver = 'ode113';"); 
			break; 
		case 4: 
			engEvalString(ep, "mySolver = 'ode15s';"); 
			break; 
		case 5: 
			engEvalString(ep, "mySolver = 'ode23s';"); 
			break; 
		case 6: 
			engEvalString(ep, "mySolver = 'ode23t';"); 
			break; 
		case 7: 
			engEvalString(ep, "mySolver = 'ode23tb';"); 
			break; 
		default : 
			engEvalString(ep, "mySolver = 'ode45';"); 
			break; 
		} 
		 
		engEvalString(ep, "opts = simset('Solver', mySolver);"); 
		 
		// 仿真 
		 
		engEvalString(ep, "sim('test01', T, opts);"); 
		engEvalString(ep, "plot(test01data.time, test01data.signals.values);"); 
		 
	} 
	else 
	{ 
		if(ep != NULL) 
			engClose(ep); 
		m_btnStart.SetWindowText("开始仿真"); 
		m_bRun = FALSE; 
	} 
	 
} 
 
 
 
void CSimulinkVCDlg::OnOK()  
{ 
	// TODO: Add extra validation here 
 
	CDialog::OnOK(); 
} 
 
 
 
 
 
BOOL CAboutDlg::OnInitDialog()  
{ 
	CDialog::OnInitDialog(); 
	 
	// TODO: Add extra initialization here 
	m_email.SetURL(_T("mailto:simonjp@etang.com")); 
	m_email.SetUnderline(FALSE); 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
}