📄 keypathview.cpp
字号:
theSql.Format("select prev_point_id, next_point_id, duttem, project_id from path_info where project_id = %s order by path_id ASC", theApp.m_PathTitleID);
m_accessRS = m_accessConn->Execute(_bstr_t(theSql), NULL, adCmdText);
while (!m_accessRS->adoEOF) {
CEdgeInfo eInfo;
int prevID = atoi((LPCTSTR)(_bstr_t) m_accessRS->GetCollect("prev_point_id"));
int nextID = atoi((LPCTSTR)(_bstr_t) m_accessRS->GetCollect("next_point_id"));
eInfo.prevPoint = &pointVec[prevID - 1];
eInfo.nextPoint = &pointVec[nextID - 1];
eInfo.duttem = atoi((LPCTSTR)(_bstr_t) m_accessRS->GetCollect("duttem"));
edgeVec.push_back(eInfo);
m_accessRS->MoveNext();
}
prev = -1;
next = -1;
ReDrawAll();
if (m_accessConn != NULL)
{
if (m_accessConn->State)
{
m_accessConn->Close();
}
m_accessConn = NULL;
}
if (m_accessRS != NULL)
{
if (m_accessRS->State)
{
m_accessRS->Close();
}
m_accessRS = NULL;
}
}
}
}
void CKeyPathView::OnFileSave()
{
if (theApp.m_PathTitleID.GetLength() > 0) {
//获取当前程序所在物理路径
TCHAR exeFullPath[MAX_PATH];
CString theConnUdl;
GetCurrentDirectory(MAX_PATH, exeFullPath);
//数据库连接指针
_ConnectionPtr m_accessConn;
//数据库结果集指针
_RecordsetPtr m_accessRS;
//初始化数据库连接
//theConnUdl.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s\\key_path.mdb;Jet OLEDB:Database Password=;", exeFullPath);
theConnUdl.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s\\key_path.mdb", exeFullPath);
HRESULT hr;
try
{
hr = m_accessConn.CreateInstance("ADODB.Connection"); // 创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_accessConn->Open((_bstr_t) theConnUdl, "", "", adModeUnknown);
}
}
catch(_com_error e)// 捕捉异常
{
MessageBox("连接数据库失败");
return;
}
//初始化数据库
m_accessRS.CreateInstance(_uuidof(Recordset));
m_accessRS->CursorType = adOpenStatic;
m_accessRS->CursorLocation = adUseClient;
CString theSql;
int i = 0;
//获取工程id
theSql.Format("select id from project where project_name = '%s'", theApp.m_PathTitle);
m_accessRS = m_accessConn->Execute(_bstr_t(theSql), NULL, adCmdText);
if (!m_accessRS->adoEOF) {
CString projectID = (LPCTSTR)(_bstr_t) m_accessRS->GetCollect("id");
//删除原来存储的顶点信息
theSql.Format("delete from point_info where project_id = %s", projectID);
m_accessRS = m_accessConn->Execute(_bstr_t(theSql), NULL, adCmdText);
//删除原来存储的路径信息
theSql.Format("delete from path_info where project_id = %s", projectID);
m_accessRS = m_accessConn->Execute(_bstr_t(theSql), NULL, adCmdText);
//保存顶点信息
for (i = 0; i < pointVec.size(); i++) {
theSql.Format("insert into point_info ([point_id], [name], [memo], [radius], [x_axis], [y_axis], [project_id]) values (%d, '%s', '%s', %d, %d, %d, %s)",
pointVec[i].id, pointVec[i].name, pointVec[i].memo, pointVec[i].radius, pointVec[i].xAxis, pointVec[i].yAxis, projectID);
m_accessRS = m_accessConn->Execute(_bstr_t(theSql), NULL, adCmdText);
}
//保存路径信息
for (i = 0; i < edgeVec.size(); i++) {
theSql.Format("insert into path_info ([path_id], [prev_point_id], [next_point_id], [duttem], [project_id]) values (%d, %d, %d, %d, %s)",
(i + 1), edgeVec[i].prevPoint->id , edgeVec[i].nextPoint->id, edgeVec[i].duttem, projectID);
m_accessRS = m_accessConn->Execute(_bstr_t(theSql), NULL, adCmdText);
}
}
if (m_accessConn != NULL)
{
if (m_accessConn->State)
{
m_accessConn->Close();
}
m_accessConn = NULL;
}
if (m_accessRS != NULL)
{
if (m_accessRS->State)
{
m_accessRS->Close();
}
m_accessRS = NULL;
}
} else {
CProjectNameDlg dlg;
if (dlg.DoModal() == IDOK) {
//获取当前程序所在物理路径
TCHAR exeFullPath[MAX_PATH];
CString theConnUdl;
GetCurrentDirectory(MAX_PATH, exeFullPath);
//数据库连接指针
_ConnectionPtr m_accessConn;
//数据库结果集指针
_RecordsetPtr m_accessRS;
//初始化数据库连接
//theConnUdl.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s\\key_path.mdb;Jet OLEDB:Database Password=;", exeFullPath);
theConnUdl.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s\\key_path.mdb", exeFullPath);
HRESULT hr;
try
{
hr = m_accessConn.CreateInstance("ADODB.Connection"); // 创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_accessConn->Open((_bstr_t) theConnUdl, "", "", adModeUnknown);
}
}
catch(_com_error e)// 捕捉异常
{
MessageBox("连接数据库失败");
return;
}
//初始化数据库
m_accessRS.CreateInstance(_uuidof(Recordset));
m_accessRS->CursorType = adOpenStatic;
m_accessRS->CursorLocation = adUseClient;
CString theSql;
int i = 0;
//验证工程名称是否存在
theSql.Format("select id from project where project_name = '%s'", theApp.m_PathTitle);
m_accessRS = m_accessConn->Execute(_bstr_t(theSql), NULL, adCmdText);
if (!m_accessRS->adoEOF) {
MessageBox("工程名称已经存在!名称不能重复。");
return;
}
try {
//保存工程信息
theSql.Format("insert into project ([project_name], [memo]) values ('%s', ' ') ", theApp.m_PathTitle);
m_accessRS = m_accessConn->Execute(_bstr_t(theSql), NULL, adCmdText);
} catch (_com_error e) {
CString errormessage;
errormessage.Format("连接数据库失败!rn错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
return ;
}
//获取工程id
theSql.Format("select id from project where project_name = '%s'", theApp.m_PathTitle);
m_accessRS = m_accessConn->Execute(_bstr_t(theSql), NULL, adCmdText);
if (!m_accessRS->adoEOF) {
CString projectID = (LPCTSTR)(_bstr_t) m_accessRS->GetCollect("id");
//保存顶点信息
for (i = 0; i < pointVec.size(); i++) {
theSql.Format("insert into point_info ([point_id], [name], [memo], [radius], [x_axis], [y_axis], [project_id]) values (%d, '%s', '%s', %d, %d, %d, %s)",
pointVec[i].id, pointVec[i].name, pointVec[i].memo, pointVec[i].radius, pointVec[i].xAxis, pointVec[i].yAxis, projectID);
m_accessRS = m_accessConn->Execute(_bstr_t(theSql), NULL, adCmdText);
}
//保存路径信息
for (i = 0; i < edgeVec.size(); i++) {
theSql.Format("insert into path_info ([path_id], [prev_point_id], [next_point_id], [duttem], [project_id]) values (%d, %d, %d, %d, %s)",
(i + 1), edgeVec[i].prevPoint->id , edgeVec[i].nextPoint->id, edgeVec[i].duttem, projectID);
m_accessRS = m_accessConn->Execute(_bstr_t(theSql), NULL, adCmdText);
}
}
if (m_accessConn != NULL)
{
if (m_accessConn->State)
{
m_accessConn->Close();
}
m_accessConn = NULL;
}
if (m_accessRS != NULL)
{
if (m_accessRS->State)
{
m_accessRS->Close();
}
m_accessRS = NULL;
}
}
}
}
void CKeyPathView::OnFileSaveAs()
{
CProjectNameDlg dlg;
if (dlg.DoModal() == IDOK) {
//获取当前程序所在物理路径
TCHAR exeFullPath[MAX_PATH];
CString theConnUdl;
GetCurrentDirectory(MAX_PATH, exeFullPath);
//数据库连接指针
_ConnectionPtr m_accessConn;
//数据库结果集指针
_RecordsetPtr m_accessRS;
//初始化数据库连接
//theConnUdl.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s\\key_path.mdb;Jet OLEDB:Database Password=;", exeFullPath);
theConnUdl.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s\\key_path.mdb", exeFullPath);
HRESULT hr;
try
{
hr = m_accessConn.CreateInstance("ADODB.Connection"); // 创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_accessConn->Open((_bstr_t) theConnUdl, "", "", adModeUnknown);
}
}
catch(_com_error e)// 捕捉异常
{
MessageBox("连接数据库失败");
return;
}
//初始化数据库
m_accessRS.CreateInstance(_uuidof(Recordset));
m_accessRS->CursorType = adOpenStatic;
m_accessRS->CursorLocation = adUseClient;
CString theSql;
int i = 0;
//验证工程名称是否存在
theSql.Format("select id from project where project_name = '%s'", theApp.m_PathTitle);
m_accessRS = m_accessConn->Execute(_bstr_t(theSql), NULL, adCmdText);
if (!m_accessRS->adoEOF) {
MessageBox("工程名称已经存在!名称不能重复。");
return;
}
try {
//保存工程信息
theSql.Format("insert into project ([project_name], [memo]) values ('%s', ' ') ", theApp.m_PathTitle);
//MessageBox(theSql);
m_accessRS = m_accessConn->Execute(_bstr_t(theSql), NULL, adCmdText);
} catch (_com_error e) {
CString errormessage;
errormessage.Format("连接数据库失败!rn错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
return ;
}
//获取工程id
theSql.Format("select id from project where project_name = '%s'", theApp.m_PathTitle);
m_accessRS = m_accessConn->Execute(_bstr_t(theSql), NULL, adCmdText);
if (!m_accessRS->adoEOF) {
CString projectID = (LPCTSTR)(_bstr_t) m_accessRS->GetCollect("id");
//保存顶点信息
for (i = 0; i < pointVec.size(); i++) {
theSql.Format("insert into point_info ([point_id], [name], [memo], [radius], [x_axis], [y_axis], [project_id]) values (%d, '%s', '%s', %d, %d, %d, %s)",
pointVec[i].id, pointVec[i].name, pointVec[i].memo, pointVec[i].radius, pointVec[i].xAxis, pointVec[i].yAxis, projectID);
m_accessRS = m_accessConn->Execute(_bstr_t(theSql), NULL, adCmdText);
}
//保存路径信息
for (i = 0; i < edgeVec.size(); i++) {
theSql.Format("insert into path_info ([path_id], [prev_point_id], [next_point_id], [duttem], [project_id]) values (%d, %d, %d, %d, %s)",
(i + 1), edgeVec[i].prevPoint->id , edgeVec[i].nextPoint->id, edgeVec[i].duttem, projectID);
m_accessRS = m_accessConn->Execute(_bstr_t(theSql), NULL, adCmdText);
}
}
if (m_accessConn != NULL)
{
if (m_accessConn->State)
{
m_accessConn->Close();
}
m_accessConn = NULL;
}
if (m_accessRS != NULL)
{
if (m_accessRS->State)
{
m_accessRS->Close();
}
m_accessRS = NULL;
}
}
}
void CKeyPathView::OnButtonSelCalResult()
{
drawType = 40;
m_resultList.ResetContent();
m_varList.ResetContent();
int i = 0;
for (i = 0; i < pointVec.size(); i++)
pointVec[i].isKey = 0;
for (i = 0; i < edgeVec.size(); i++)
edgeVec[i].isKey = 0;
prev = -1;
next = -1;
startCalPoint = -1;
endCalPoint = -1;
}
void CKeyPathView::CalKeyPath()
{
int total = 0;
int i = 0;
for (i = startCalPoint; i <= endCalPoint; i++) {
CPointInfo pInfo;
pInfo.xAxis = pointVec[i].xAxis;
pInfo.yAxis = pointVec[i].yAxis;
pInfo.color = pointColor;
pInfo.name = pointVec[i].name;
pInfo.memo = pointVec[i].memo;
pInfo.id = pointVec[i].id;
selPointVec.push_back(pInfo);
}
for (i = 0; i < edgeVec.size(); i++) {
CEdgeInfo eInfo = edgeVec[i];
if (eInfo.prevPoint->id - 1 >= startCalPoint && eInfo.prevPoint->id - 1 <= endCalPoint
&& eInfo.nextPoint->id - 1 >= startCalPoint && eInfo.nextPoint->id - 1 <= endCalPoint) {
CEdgeInfo eInfo1;
eInfo1.prevPoint = &selPointVec[eInfo.prevPoint->id - startCalPoint - 1];
eInfo1.nextPoint = &selPointVec[eInfo.nextPoint->id - startCalPoint - 1];
eInfo1.duttem = eInfo.duttem;
selEdgeVec.push_back(eInfo1);
}
}
edgenode *p;
vexnode* graphicmap = (vexnode*) malloc(pointVec.size() * sizeof(vexnode));
//初始化默认值
for(i = 0; i <= selPointVec.size(); i++)
{
graphicmap[i].trafficnetname = selPointVec[i].id - 1;
graphicmap[i].id = 0;
graphicmap[i].link = NULL;
}
//构建关键路径计算数值
for(i = 0; i < selEdgeVec.size(); i++)
{
CEdgeInfo eInfo = selEdgeVec[i];
p = (edgenode*) malloc(sizeof(edgenode));
p->adjvex = eInfo.nextPoint->id - startCalPoint - 1;
p->dut = eInfo.duttem;
graphicmap[eInfo.nextPoint->id - startCalPoint - 1].id++;
p->next = graphicmap[eInfo.prevPoint->id - startCalPoint - 1].link ;
graphicmap[eInfo.prevPoint->id - startCalPoint - 1].link = p;
}
StepRun(1);
for (i = 0; i < pointVec.size(); i++)
pointVec[i].isKey = 0;
for (i = 0; i < edgeVec.size(); i++)
edgeVec[i].isKey = 0;
//寻找关键路径
SearchKeyPath(graphicmap, selPointVec.size(), selEdgeVec.size(), total);
//显示最后结果
CString tmp;
for (i = startCalPoint; i <= endCalPoint; i++) {
if (pointVec[i].isKey == 1) {
tmp += pointVec[i].name;
tmp += ";";
}
}
m_resultList.AddString(tmp);
tmp.Format("关键路径总长度:%d", total);
m_resultList.AddString(tmp);
prev = -1;
next = -1;
startCalPoint = -1;
endCalPoint = -1;
selPointVec.clear();
selEdgeVec.clear();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -