www.pudn.com > tab2ESRIshapefile.zip > Tab2ShpDlg.cpp


// Tab2ShpDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Tab2Shp.h" 
#include "Tab2ShpDlg.h" 
 
#include  
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// 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) 
	//}}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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CTab2ShpDlg dialog 
 
CTab2ShpDlg::CTab2ShpDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CTab2ShpDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CTab2ShpDlg) 
	m_strImut = _T("C:\\Program Files\\MapInfo\\Professional\\UT"); 
	m_strDstPath = _T(""); 
	m_strSrcPath = _T(""); 
	m_nSingleFile = FALSE; 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
	//m_ctrlEditImut.SetSearchType(SEARCH_FILE); 
} 
 
void CTab2ShpDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CTab2ShpDlg) 
	DDX_Control(pDX, IDC_PROGRESS2, m_ctrlProgress); 
	DDX_Control(pDX, IDC_EDIT_IMUT, m_ctrlEditImut); 
	DDX_Control(pDX, IDC_EDIT_SRC, m_ctrlEditSrc); 
	DDX_Control(pDX, IDC_EDIT_DST, m_ctrlEditDst); 
	DDX_Text(pDX, IDC_EDIT_IMUT, m_strImut); 
	DDV_MaxChars(pDX, m_strImut, 256); 
	DDX_Text(pDX, IDC_EDIT_DST, m_strDstPath); 
	DDV_MaxChars(pDX, m_strDstPath, 256); 
	DDX_Text(pDX, IDC_EDIT_SRC, m_strSrcPath); 
	DDV_MaxChars(pDX, m_strSrcPath, 256); 
	DDX_Check(pDX, IDC_SINGLEFILE, m_nSingleFile); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CTab2ShpDlg, CDialog) 
	//{{AFX_MSG_MAP(CTab2ShpDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_START, OnStart) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CTab2ShpDlg message handlers 
 
BOOL CTab2ShpDlg::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 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CTab2ShpDlg::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 CTab2ShpDlg::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 CTab2ShpDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CTab2ShpDlg::OnStart()  
{ 
	UpdateData( TRUE ); 
	BeginWaitCursor(); 
	 
	char *szExts[8] = {".shp", ".tab", "", "", ".dgn", ".mem", ".gml", ".dsn"}; 
	if( m_strImut.IsEmpty()) 
	{ 
		MessageBox( "请选择imut.exe所在目录", "系统警告" ); 
		return; 
	} 
	if( m_strSrcPath.IsEmpty()) 
	{ 
		MessageBox( "请选择输入目录", "系统警告" ); 
		return; 
	} 
	if( m_strDstPath.IsEmpty()) 
	{ 
		MessageBox( "请选择输出目录", "系统警告" ); 
		return; 
	} 
	int nIndex = m_strDstPath.GetLength()-1; 
	if( m_strDstPath[nIndex] == '\\' ) 
		m_strDstPath.SetAt(nIndex, 0); 
	 
	BeginWaitCursor(); 
	m_ctrlProgress.SetRange( 0, 100 ); 
	m_ctrlProgress.SetPos( 0 ); 
	m_ctrlProgress.SetPos( 100 ); 
 
	Convert0(m_strSrcPath, m_strDstPath); 
 
	EndWaitCursor(); 
	MessageBox( "\n          转换成功!          \n", "系统提示"); 
} 
 
BOOL CTab2ShpDlg::Convert0(CString strFilePath, CString strDstPath) 
{ 
	CFileStatus fileStatus; 
	CFile::GetStatus( strFilePath, fileStatus ); 
	if ( fileStatus.m_attribute & CFile::directory ) 
	{ 
		CString strFilter = strFilePath + "\\*."; 
		CFileFind findFile; 
		BOOL bFind = findFile.FindFile( strFilter ); 
		while( bFind) 
		{ 
			bFind = findFile.FindNextFile(); 
			if (findFile.IsDots()) 
				continue; 
 
			if ( findFile.IsDirectory() ) 
			{ 
				CString strPath = findFile.GetFilePath(); 
				CString strDstPath1 = m_strDstPath+ 
					strPath.Right(strPath.GetLength()-m_strSrcPath.GetLength()); 
				MakeDir( strDstPath1 ); 
				Convert0(strPath, strDstPath1); 
			} 
		} 
 
		if ( m_nSingleFile ) 
		{ 
			strFilter = strFilePath + "\\*.tab"; 
			bFind = findFile.FindFile( strFilter ); 
			while( bFind) 
			{ 
				bFind = findFile.FindNextFile(); 
				CString strFile = findFile.GetFileName(); 
				CString strFileTitle = findFile.GetFileTitle(); 
				GetDlgItem( IDC_MSG )->SetWindowText(strFile); 
				 
				Convert( findFile.GetFilePath(), strDstPath ); 
				 
				//			CFileStatus fileStatus; 
				//			CFile::GetStatus( strDir1 + "\\" + strFile, fileStatus ); 
				//			nSize += fileStatus.m_size; 
				//			int nPos = nSize*100/nTotleSize; 
				//			m_ctrlProgress.SetPos( nPos ); 
			} 
		} 
		else 
		{ 
			strFilter = strFilePath + "\\*.tab"; 
			bFind = findFile.FindFile( strFilter ); 
			if( bFind) 
			{ 
				GetDlgItem( IDC_MSG )->SetWindowText(strFilePath); 
				Convert2( strFilePath, strDstPath ); 
			} 
 
		} 
	} 
	else 
	{ 
		Convert( strFilePath, strDstPath ); 
	} 
	return TRUE; 
} 
 
BOOL CTab2ShpDlg::Convert(CString strFilePath, CString strDstPath) 
{ 
	BOOL bReturn = FALSE; 
	CString strSrcPath; 
	CString strFileName; 
	int nIndex = strFilePath.ReverseFind('\\'); 
	if ( nIndex >= 0 ) 
	{ 
		strSrcPath = strFilePath.Left(nIndex); 
		strFileName = strFilePath.Right( strFilePath.GetLength()-nIndex-1); 
		strFileName = strFileName.Left( strFileName.GetLength()-4); 
	} 
	else 
	{ 
		strSrcPath = strFilePath; 
		strFileName = strFilePath; 
		strFileName = strFileName.Left( strFileName.GetLength()-4); 
	} 
 
	char szExeFile[1024]; 
	sprintf(szExeFile, "\"%s\\imut.exe\" CFGenerate MAPINFO SHAPE \"%s/\" hggtemp1.dat LOG_STANDARDOUT YES +ID %s",  
		(LPCTSTR)m_strImut,(LPCTSTR)strSrcPath, (LPCTSTR)strFileName); 
 
	STARTUPINFO si; 
	GetStartupInfo( &si ); 
	PROCESS_INFORMATION pi; 
	si.dwFlags = STARTF_FORCEONFEEDBACK; 
	si.wShowWindow = SW_HIDE; 
	 
	if(CreateProcess(NULL, 
		szExeFile, 
		NULL, 
		NULL, 
		FALSE, 
		CREATE_DEFAULT_ERROR_MODE, 
		NULL, 
		(LPCTSTR)m_strImut, 
		&si, 
		&pi)) 
	{ 
		// 关闭子进程的主线程句柄 
		CloseHandle(pi.hThread); 
		//等待子进程的退出 
		WaitForSingleObject(pi.hProcess, INFINITE); 
		// 获取子进程的退出码 
		//GetExitCodeProcess(pi.hProcess, &dwExitCode); 
		// 关闭子进程句柄 
		CloseHandle(pi.hProcess); 
		 
		FILE *fp; 
		sprintf(szExeFile, "%s\\hggtemp2.dat",(LPCTSTR)m_strImut); 
		if((fp = fopen(szExeFile, "wt")) != NULL) 
		{ 
			fprintf( fp, "MACRO _EXTENSION TAB\n"); 
			fprintf( fp, "MACRO _BASENAME %s\n", (LPCTSTR)strFileName); 
			fprintf( fp, "MACRO _FULLBASENAME %s\n", (LPCTSTR)strFileName); 
			fprintf( fp, "MACRO SourceDataset %s\n", (LPCTSTR)strSrcPath); 
			fprintf( fp, "MACRO DestDataset %s\n", strDstPath); 
			fprintf( fp, "INCLUDE hggtemp1.dat\n"); 
			fprintf( fp, "MAPINFO_IDs %s\n", (LPCTSTR)strFileName); 
			fprintf( fp, "MAPINFO_IN_IDs %s\n", (LPCTSTR)strFileName); 
			fclose(fp); 
 
			sprintf(szExeFile, "\"%s\\imut.exe\" hggtemp2.dat", (LPCTSTR)m_strImut); 
			if(CreateProcess(NULL, 
				szExeFile, 
				NULL, 
				NULL, 
				FALSE, 
				CREATE_DEFAULT_ERROR_MODE, 
				NULL, 
				(LPCTSTR)m_strImut, 
				&si, 
				&pi)) 
			{ 
				// 关闭子进程的主线程句柄 
				CloseHandle(pi.hThread); 
				//等待子进程的退出 
				WaitForSingleObject(pi.hProcess, INFINITE); 
				// 获取子进程的退出码 
				//GetExitCodeProcess(pi.hProcess, &dwExitCode); 
				// 关闭子进程句柄 
				CloseHandle(pi.hProcess); 
				bReturn = TRUE; 
			} 
		} 
	} 
	return bReturn; 
} 
 
BOOL CTab2ShpDlg::Convert2(CString strFilePath, CString strDstPath) 
{ 
	BOOL bReturn = FALSE; 
	char szExeFile[1024]; 
	sprintf(szExeFile, "\"%s\\imut.exe\" CFGenerate MAPINFO SHAPE \"%s/\" hggtemp1.dat LOG_STANDARDOUT YES",  
		(LPCTSTR)m_strImut,(LPCTSTR)strFilePath); 
	 
	STARTUPINFO si; 
	GetStartupInfo( &si ); 
	PROCESS_INFORMATION pi; 
	si.dwFlags = STARTF_FORCEONFEEDBACK; 
	si.wShowWindow = SW_HIDE; 
	 
	if(CreateProcess(NULL, 
		szExeFile, 
		NULL, 
		NULL, 
		FALSE, 
		CREATE_DEFAULT_ERROR_MODE, 
		NULL, 
		(LPCTSTR)m_strImut, 
		&si, 
		&pi)) 
	{ 
		// 关闭子进程的主线程句柄 
		CloseHandle(pi.hThread); 
		//等待子进程的退出 
		WaitForSingleObject(pi.hProcess, INFINITE); 
		// 获取子进程的退出码 
		//GetExitCodeProcess(pi.hProcess, &dwExitCode); 
		// 关闭子进程句柄 
		CloseHandle(pi.hProcess); 
		 
		FILE *fp; 
		sprintf(szExeFile, "%s\\hggtemp2.dat",(LPCTSTR)m_strImut); 
		if((fp = fopen(szExeFile, "wt")) != NULL) 
		{ 
			fprintf( fp, "MACRO _EXTENSION TAB\n"); 
			fprintf( fp, "MACRO SourceDataset %s\n", (LPCTSTR)strFilePath); 
			fprintf( fp, "MACRO DestDataset %s\n", strDstPath); 
			fprintf( fp, "INCLUDE hggtemp1.dat\n"); 
			fclose(fp); 
			 
			sprintf(szExeFile, "\"%s\\imut.exe\" hggtemp2.dat", (LPCTSTR)m_strImut); 
			if(CreateProcess(NULL, 
				szExeFile, 
				NULL, 
				NULL, 
				FALSE, 
				CREATE_DEFAULT_ERROR_MODE, 
				NULL, 
				(LPCTSTR)m_strImut, 
				&si, 
				&pi)) 
			{ 
				// 关闭子进程的主线程句柄 
				CloseHandle(pi.hThread); 
				//等待子进程的退出 
				WaitForSingleObject(pi.hProcess, INFINITE); 
				// 获取子进程的退出码 
				//GetExitCodeProcess(pi.hProcess, &dwExitCode); 
				// 关闭子进程句柄 
				CloseHandle(pi.hProcess); 
				bReturn = TRUE; 
			} 
		} 
	} 
	return bReturn; 
} 
 
void CTab2ShpDlg::MakeDir(CString strPath) 
{ 
	int nIndex = strPath.Find(m_strDstPath); 
	if ( nIndex >= 0 ) 
	{ 
		_chdir(m_strDstPath); 
		MakeDir( strPath.Right(strPath.GetLength()-m_strDstPath.GetLength()-1)); 
	} 
	else 
	{ 
		CFileFind findFile; 
		nIndex = strPath.Find('\\'); 
		if ( nIndex >= 0 ) 
		{ 
			CString strLeft = strPath.Left(nIndex); 
			BOOL bFind = findFile.FindFile( strLeft ); 
			if ( !bFind) 
			{ 
				mkdir(strLeft); 
			} 
			else 
			{ 
				_chdir(strLeft); 
			} 
			MakeDir( strPath.Right(strPath.GetLength()-nIndex-1)); 
		} 
		else 
		{ 
			BOOL bFind = findFile.FindFile( strPath ); 
			if ( !bFind) 
			{ 
				mkdir(strPath); 
			} 
			else 
			{ 
				_chdir(strPath); 
			} 
		} 
	} 
}