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

📄 filetrandlg.cpp

📁 该代码为进行DTM数据点采集时,作为VZ的外挂程序,可以进行约束匹配
💻 CPP
字号:
// FileTranDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FileTran.h"
#include "FileTranDlg.h"
#include "math.h"

#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()

/////////////////////////////////////////////////////////////////////////////
// CFileTranDlg dialog

CFileTranDlg::CFileTranDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFileTranDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFileTranDlg)
	m_code = 8310;
	m_source_file = _T("");
	m_save_file = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFileTranDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFileTranDlg)
	DDX_Text(pDX, IDC_CODE, m_code);
	DDX_Text(pDX, IDC_EDIT1, m_source_file);
	DDX_Text(pDX, IDC_EDIT2, m_save_file);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFileTranDlg, CDialog)
	//{{AFX_MSG_MAP(CFileTranDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_TRAN, OnTran)
	ON_BN_CLICKED(IDC_SOURCE, OnSource)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_BN_CLICKED(ID_TRAN_BREAKLINE, OnTranBreakline)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFileTranDlg message handlers

BOOL CFileTranDlg::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 CFileTranDlg::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 CFileTranDlg::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 CFileTranDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CFileTranDlg::OnTran() 
{
	// TODO: Add your control notification handler code here

	UpdateData(FALSE);

	// 获取文件路径
	char fileName_source[256];
	char fileName_save[256];
	char tempChar[128];
	FILE* fileIn;
	FILE* fileOut;
	int   temp,sum,i;
	double dl;
	long  pointSum=0;
	double x,y,z;

	// 虚拟文件路径
	strcpy(fileName_source,m_source_file);
	strcpy(fileName_save,m_save_file);
	if((fileIn=fopen(fileName_source,"r"))==FALSE)
	{
		AfxMessageBox("source file fail to open!");
		return;
	}
	if((fileOut=fopen(fileName_save,"w"))==FALSE)
	{
		AfxMessageBox("saving file fail to open!");
		fclose(fileIn);
		return;
	}

	fgets(tempChar,128,fileIn);

	// 进入循环,读取每一行的第一个数,如果是引出层代码就读坐标并写入对应的文件中去
	while(!feof(fileIn))
	{
		fscanf(fileIn,"%lf",&dl);
		if(fabs(dl-m_code)>0.000001)
		{
			fgets(tempChar,128,fileIn);
			continue;
		}
		else
		{
			// 读取对应的数值
			fscanf(fileIn,"%d",&temp);
			// 点的个数
			fscanf(fileIn,"%d",&sum);
			fscanf(fileIn,"%d",&temp);

			// 将该快的点读入并写入到文件中去
			for(i=0; i<sum; i++)
			{
				fscanf(fileIn,"%lf",&x);
				fscanf(fileIn,"%lf",&y);
				fscanf(fileIn,"%lf",&z);
				fscanf(fileIn,"%d",&temp);

				// 写入到文件中去
				fprintf(fileOut,"%12.6lf %12.6lf %12.6lf\n",x,y,z);

				// 点的个数加
				pointSum++;
			}
		}
	}

	// 写入点的个数
	fprintf(fileOut,"%ld",pointSum);

	// 关闭文件
	fclose(fileIn);
	fclose(fileOut);

	
}

void CFileTranDlg::OnSource() 
{
	// TODO: Add your control notification handler code here
	CString     strOpenFileType = "源文件 (*.vvt)|*.vvt|All Files (*.*)|*.*||";
	CFileDialog FileDlg(TRUE, "*.vvt", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT, strOpenFileType);
	if(FileDlg.DoModal()==IDOK)
	{
		m_source_file = FileDlg.GetPathName();
	}
	UpdateData(FALSE);
	
}

void CFileTranDlg::OnSave() 
{
	// TODO: Add your control notification handler code here
	CString     strOpenFileType = "源文件 (*.txt)|*.blk|All Files (*.*)|*.*||";
	CFileDialog FileDlg(FALSE, "*.txt", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT, strOpenFileType);
	if(FileDlg.DoModal()==IDOK)
	{
		m_save_file = FileDlg.GetPathName();
	}
	UpdateData(FALSE);
	
}
//////////////////////////////////////////////////////////////////////////
// 该函数用于将断裂线文件转化成点文件
//////////////////////////////////////////////////////////////////////////
void CFileTranDlg::OnTranBreakline() 
{
	// TODO: Add your control notification handler code here
		UpdateData(FALSE);

	// 获取文件路径
	char fileName_source[256];
	char fileName_save[256];
	char tempChar[128];
	FILE* fileIn;
	FILE* fileOut;
	int   temp,sum,i,j;
	double dl;
	long  pointSum=0;
	double xl,yl,zl;
	double xf,yf,zf;
	int  linenum;
	double distance_line; // 折线长度
	double interval = 1.; // 内插间隔
	int    nLinePointNum; // 该折线上需内插点的个数
	double dx,dy,dz,dnz,dnx,dny;
	double x,y,z,dn;

	// 虚拟文件路径
	strcpy(fileName_source,m_source_file);
	strcpy(fileName_save,m_save_file);
	if((fileIn=fopen(fileName_source,"r"))==FALSE)
	{
		AfxMessageBox("source file fail to open!");
		return;
	}
	if((fileOut=fopen(fileName_save,"w"))==FALSE)
	{
		AfxMessageBox("saving file fail to open!");
		fclose(fileIn);
		return;
	}

	fgets(tempChar,128,fileIn);

	// 进入循环,读取每一行的第一个数,如果是引出层代码就读坐标并写入对应的文件中去
	while(!feof(fileIn))
	{
		fscanf(fileIn,"%lf",&dl);
		if(fabs(dl-m_code)>0.000001)
		{
			fgets(tempChar,128,fileIn);
			continue;
		}
		else
		{
			// 读取对应的数值
			fscanf(fileIn,"%d",&temp);
			// 点的个数
			fscanf(fileIn,"%d",&sum);
			fscanf(fileIn,"%d",&temp);

			// 计算折线的数量
			linenum = sum-1;

			// 读取前一个点的坐标
			fscanf(fileIn,"%lf",&xf);
			fscanf(fileIn,"%lf",&yf);
			fscanf(fileIn,"%lf",&zf);
			fscanf(fileIn,"%d",&temp);

			// 依次读入每条折线的后一个点,并进行插值处理
			for(i=0; i<linenum; i++)
			{
				fscanf(fileIn,"%lf",&xl);
				fscanf(fileIn,"%lf",&yl);
				fscanf(fileIn,"%lf",&zl);
				fscanf(fileIn,"%d",&temp);

				// 内插点,并将其写入文件中
				// xf,yf,zf为前一点,xl,yl,zl为后一点
				// 折线长度
				dx = xl-xf; dy = yl-yf; dz = zl-zf;
				distance_line = sqrt(dx*dx+dy*dy);
				dnz = dz/distance_line;
				dnx = dx/distance_line;
				dny = dy/distance_line;

				nLinePointNum = int(distance_line/interval);
				for(j=0; j<nLinePointNum; j++)
				{
					dn = j*interval;
					x = xl+dn*dnx;
					y = yl+dn*dny;
					z = zl+dn*dnz;
					// 写入文件中
					fprintf(fileOut,"%lf12.6 %12.6lf %12.6lf\n",x,y,z);

					pointSum++;
				}

			}

		}
	}

	// 写入点的个数
	fprintf(fileOut,"%ld",pointSum);

	// 关闭文件
	fclose(fileIn);
	fclose(fileOut);
	
}

⌨️ 快捷键说明

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