You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

428 lines
12 KiB
C++

#include "StdAfx.h"
#include "WorkFileMgr.h"
#include "GlobalDefine.h"
#include "TemplateMgr.h"
#include "WorkFileLable.h"
#include "LogMgr.h"
#include "ModuleDeviceMgr.h"
#include "ModuleDrawMgr.h"
#include "ObjComponentMgr.h"
#include "MsgBox.h"
#include "GlobalFunction.h"
#include "MarkAreaMgr.h"
#include "FileMgr.h"
#include "Laser.h"
#include "CommonFlowMgr.h"
#include "Propertie.h"
#include "PropertieMgr.h"
#include "AuthorityMgr.h"
#include "WorkTime.h"
#include "PenParMgr.h"
#include "ProgramLaserTuiHuo.h"
#include "WaferRecipeDataMgr.h"
#include "PropertieMgr.h"
#include "EncryptionMgr.h"
#define SAVE_PAR_FILE_PATH _T("\\SaveDevPar\\")
#define CHECK_WAFER_PT_RADIUS 160 //限制数据点的范围
#define MAP_HISTORY_DATA_PATH _T("\\MapHistory\\")
#define RECIPE_FILE_PATH _T("\\RecipePar\\")
CString CWorkFileMgr::m_CurOpenFile;
CWorkFileMgr *gWorkFileMgr = new CWorkFileMgr;
CWorkFileMgr::CWorkFileMgr(void)
{
}
CWorkFileMgr::~CWorkFileMgr(void)
{
}
void CWorkFileMgr:: OnAppInitialize()
{
}
#if 1
//是否打开文件
bool CWorkFileMgr::IsOpenFile()
{
if(m_CurOpenFile == "")
{
CMsgBox MsgBox;
MsgBox.Show("未打开文件!");
return false;
}
return true;
}
void CWorkFileMgr::OnOpenFile()
{
TCHAR szFilters[]=("MAK 文件(*.mak)|*.mak");
CFileDialog dlg(TRUE,("mak"),("MarkFile"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilters);
if(dlg.DoModal()==IDOK)
{
OnOpenFileExt(dlg.GetPathName());
}
}
void CWorkFileMgr::OnOpenFileExt(CString Path)
{
ReadWorkFile(Path);
//记录当前打开的文件名------------------------------------------
SaveFileName(Path);
CLayer &layer = gLayer;
layer.NotSelAllObj();//全不选
//重置所有笔的选择状态
gPenParMgr->ResetAllSelState();
#ifdef __DLG_PANE_PEN_PAR__
GetFrame()->m_CDlgPenPar.UpdatePar();
#endif
}
//记录打开文件的名字,并显示到标题
void CWorkFileMgr::SaveFileName(CString FileName)
{
m_CurOpenFile = FileName;
GetCurDocPtr()->SetTitle(m_CurOpenFile);
}
//保存当前打开的文档
void CWorkFileMgr::OnSaveFile()
{
int len = m_CurOpenFile.GetLength();
CString FileSuffix;//文件后缀
if(len>4)
FileSuffix = m_CurOpenFile.Right(3);
if(m_CurOpenFile == "" || (FileSuffix != "" && (FileSuffix != "mak")))//新的文件
{
//另存为
OnSaveFileAs();
}
else//保存当前打开的文件
{
SaveFile(m_CurOpenFile);
CMsgBox MsgBox;
MsgBox.Show(m_CurOpenFile+" 保存成功!");
}
}
//另存为
void CWorkFileMgr::OnSaveFileAs()
{
}
void CWorkFileMgr::SaveFile(CString FileName)
{
SaveWorkFile(FileName);
SaveFileName(FileName);
}
#endif
#if 1
void CWorkFileMgr::SaveWorkFile(CString FilePath)
{
//存储所有标签的容器
vector<CLab> LabVec;
//获取模块的标签
//gModuleDeviceMgr.SaveWorkFile(LabVec);
gPenParMgr->SaveWorkFile(LabVec);
gWaferRecipeDataMgr->SaveWorkFile(LabVec);
//标签写入到文件
SaveWorkFileExt(FilePath,LabVec);
}
//所有标签写入文件
void CWorkFileMgr::SaveWorkFileExt(CString FilePath,vector<CLab> &LabVec)
{
CFile file(FilePath,CFile::modeReadWrite|CFile::modeCreate);
CArchive ar(&file,CArchive::store);
size_t LabCnt = LabVec.size();
ar<<LabCnt;//标签的个数
vector<CLab>::iterator iter = LabVec.begin();
vector<CLab>::iterator iter_end = LabVec.end();
for(;iter!=iter_end;iter++)
{
(*iter).Serialize(ar);
}
}
#endif
#if 1
void CWorkFileMgr::ReadWorkFile(CString FilePath)
{
//读取所有内容----------------------------------------------------------
vector<CLab> LabVec;
ReadWorkFileExt(FilePath,LabVec);
if(LabVec.empty())
return;
//分离每个模块------------------------------------------------------------
CLabVecRang LabVecRang(LabVec,0,LabVec.size()-1);
vector<CLabVecRang> LabVecRangVec;
SeparateStrVec(LabVecRang,LabVecRangVec,LAB_MODULE_START,LAB_MODULE_END);
//依次处理每个模块---------------------------------------------------
vector<CLabVecRang>::iterator iter = LabVecRangVec.begin();
vector<CLabVecRang>::iterator iter_end = LabVecRangVec.end();
for(;iter!=iter_end;iter++)
{
if(gModuleDrawMgr.ReadWorkFile(*iter))
continue;//避免重复读取
gModuleDeviceMgr.ReadWorkFile(*iter);
}
}
void CWorkFileMgr::ReadWorkFileExt(CString FilePath,vector<CLab> &LabVec)
{
CFile file;
if(file.Open(FilePath,CFile::modeRead))
{
CArchive ar(&file,CArchive::load);
size_t LabCnt;
ar>>LabCnt;//先读取标签的个数
for(size_t i=0;i<LabCnt;i++)
{
CLab Lab;
Lab.Serialize(ar);
LabVec.push_back(Lab);
}
file.Close();
}
else
{
gLogMgr->WriteDebugLog("File Read Error", _LOG_ERROR);
}
}
//从LabVecRang 中根据StrLabStart 和StrLabEnd 标签提取连续的str 保存到LabVecRangVec
void CWorkFileMgr::SeparateStrVec(CLabVecRang &LabVecRang,vector<CLabVecRang> &LabVecRangVec,LAB_TYPE StrLabStart,LAB_TYPE StrLabEnd)
{
size_t StartCnt = 0;//避免嵌套的情况
size_t StartIdx = 0;
size_t EndIdx = 0;
size_t Start = LabVecRang.GetStart();
size_t End = LabVecRang.GetEnd()+1;
for(size_t i=Start;i<End;i++)
{
if(LabVecRang.GetLabType(i)==StrLabStart)
{
if(StartCnt==0)//只有第一个才记录
{
StartIdx = i+1;//记录分离开始位置
}
StartCnt++;
}
if(LabVecRang.GetLabType(i)==StrLabEnd)
{
if(StartCnt==1)
{
EndIdx = i-1;//目的是去掉判断的两个标签
//if(EndIdx>StartIdx)//不要搜集空的范围
{
CLabVecRang Rang(LabVecRang.GetVec(),StartIdx,EndIdx);
LabVecRangVec.push_back(Rang);
}
}
StartCnt--;
}
}
}
void CWorkFileMgr::SeparateLabVecRangVec(vector<CLab> &LabVec,vector<CLabVecRang>&LabVecRangVec,CString Path,LAB_TYPE StrLabStart,LAB_TYPE StrLabEnd)
{
CFileMgr FileMgr;
if(!FileMgr.IsFileExist(Path))
return;
ReadWorkFileExt(Path,LabVec);
if(LabVec.empty())
return;
//分离每个记录------------------------------------------------------------
CLabVecRang LabVecRang(LabVec,0,LabVec.size()-1);
SeparateStrVec(LabVecRang,LabVecRangVec,StrLabStart,StrLabEnd);
}
//在StrVec 查找和标签LabStr 匹配的字符串,并且提取标签的值,没有值返回""
CLab CWorkFileMgr::FindLab(CLabVecRang &LabVecRang,LAB_TYPE LabStr)
{
CLab ret;
size_t Start = LabVecRang.GetStart();
size_t End = LabVecRang.GetEnd()+1;
for(size_t i=Start;i<End;i++)
{
if(LabVecRang.GetLabType(i)==LabStr)
{
ret = LabVecRang.GetLab(i);
break;
}
}
return ret;
}
#endif
#if 1
//打开obj 文件
void CWorkFileMgr::OpenObjFile()
{
TCHAR szFilters[]=("OBJ 文件(*.obj)|*.obj");
CFileDialog dlg(TRUE,("obj"),("ObjFile"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilters);
//默认路径
CString Path = gProgramLaserTuiHuo->GetScanAreaFilePath();
dlg.m_ofn.lpstrInitialDir = Path;
if(dlg.DoModal()==IDOK)
{
ReadObjTemplateFile(dlg.GetPathName());
m_pView->RefreshView();
}
}
//保存obj 文件
void CWorkFileMgr::SaveObjFile()
{
CObjContainer &LayerObjContainer = gLayer.GetObjContainer();
if(!LayerObjContainer.CheckPtRadius(CHECK_WAFER_PT_RADIUS)&&!gEncryptionMgr->IsbRunAtNoteBook())
{
CString s;
s.Format("数据点不在有效范围之内,直径= %d",(CHECK_WAFER_PT_RADIUS*2));
CMsgBox MsgBox;
MsgBox.Show(s);
return;
}
TCHAR szFilters[]=("OBJ 文件(*.obj)|*.obj");
CFileDialog dlg(FALSE,("obj"),("ObjFile"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilters);
//默认路径
dlg.m_ofn.lpstrInitialDir = gProgramLaserTuiHuo->GetScanAreaFilePath();
if(dlg.DoModal()==IDOK)
{
SaveObjTemplateFile(dlg.GetPathName());
}
}
//创建当前Map 记录的路径
CString CWorkFileMgr::CreatMapHistoryPath()
{
CString MonitoringDataPath = gProgramLaserTuiHuo->GetLaipuLaserDataDir(MAP_HISTORY_DATA_PATH);
CString Path = MonitoringDataPath;
CFileMgr FileMgr;
CWorkTime WorkTime;
Path += WorkTime.GetCurYear();
Path += "\\"+WorkTime.GetCurMonth();
Path += "\\"+WorkTime.GetCurDay();
Path += "\\";
if(!FileMgr.IsDirectoryExists(Path))
{
FileMgr.CreatDir(Path);
}
return Path;
}
#endif
#if 1
//从Txt 文件中导入obj
void CWorkFileMgr::OpenObjTxtFile()
{
TCHAR szFilters[]=("TXT 文件(*.txt)|*.txt");
CFileDialog dlg(TRUE,("txt"),("TxtFile"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilters);
//默认路径
CString Path = gProgramLaserTuiHuo->GetObjTxtFilePath();
dlg.m_ofn.lpstrInitialDir = Path;
if(dlg.DoModal()==IDOK)
{
gLayer.DelAllObj();
CObjContainer &LayerObjContainer = gLayer.GetObjContainer();
LayerObjContainer.ReadObjFromTxt(dlg.GetPathName());
m_pView->RefreshView();
}
}
//保存obj 导txt 文件
void CWorkFileMgr::SaveObjTxtFile()
{
TCHAR szFilters[]=("TXT 文件(*.txt)|*.txt");
CFileDialog dlg(FALSE,("txt"),("TxtFile"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilters);
//默认路径
dlg.m_ofn.lpstrInitialDir = gProgramLaserTuiHuo->GetObjTxtFilePath();
if(dlg.DoModal()==IDOK)
{
CObjContainer &LayerObjContainer = gLayer.GetObjContainer();
LayerObjContainer.SaveObjToTxt(dlg.GetPathName());
}
}
#endif
#if 1
void CWorkFileMgr::ExtParToTxtFile()
{
TCHAR szFilters[]=("TXT 文件(*.txt)|*.txt");
CString FileName;//默认文件名
FileName += gWorkTime.GetDateTime("_","_");
FileName = "DevicePar_"+FileName;
CFileDialog dlg(FALSE,("txt"),FileName,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilters);
//默认路径
if(dlg.DoModal()==IDOK)
{
gDevicePropertieMgr.SaveAllPropertieToTxtFile(dlg.GetPathName());
}
}
//软件打开的时候导出参数到文件
void CWorkFileMgr::ExtParToTxtFileOnAppOpen()
{
CString DirPath = gProgramLaserTuiHuo->GetLaipuLaserDataDir(SAVE_PAR_FILE_PATH);
CFileMgr FileMgr;
if(!FileMgr.IsDirectoryExists(DirPath))
FileMgr.CreatDir(DirPath);
CString FileName;//默认文件名
FileName += gWorkTime.GetDateTime("_","_");
FileName = "DevicePar_"+FileName;
FileName += ".bin";
CString FilePath = DirPath + FileName;
gDevicePropertieMgr.SaveAllPropertieToTxtFile(FilePath);
CString Log;
Log = "ExtPar-->";
Log += FileName;
gLogMgr->WriteDebugLog(Log);
}
//通过文件名打开obj 文件(相对路径)
void CWorkFileMgr::ReadObjFileByName(CString FileName)
{
CFileMgr FileMgr;
CString FilePath;//完整路径
FileMgr.GetFullFilePath(FilePath,FileName);
ReadObjTemplateFile(FilePath);
}
//通过文件名打开obj 文件(绝对路径)
void CWorkFileMgr::ReadObjTemplateFile(CString FilePath)
{
ReadWorkFile(FilePath);
}
//只保存obj 模板数据
void CWorkFileMgr::SaveObjTemplateFile(CString FilePath)
{
CString MapHistoryPath = CreatMapHistoryPath();
CFileMgr FileMgr;
bool bFileExist = FileMgr.IsFileExist(FilePath);
CString FileName = FileMgr.GetFileNameFromPath(FilePath,true);
CString s = "[";
CWorkTime WorkTime;
s += WorkTime.GetCurTime("_");
s += "_";
s += gAuthorityMgr->GetCurUserName();
s += "]";
FileName = s +FileName;
FileName += ".obj";
MapHistoryPath += FileName;
if(bFileExist)//如果obj 文件本来就存在,复制到history
{
FileMgr.CopyFolder(FilePath,MapHistoryPath);
}
vector<CLab> LabVec;//存储所有标签的容器
//搜集所有标签
gTemplateMgr->SaveWorkFile(LabVec);
//所有标签写入文件
SaveWorkFileExt(FilePath,LabVec);
if(!bFileExist)//如果obj 文件不存在,复制到history (表示新建obj)
{
FileMgr.CopyFolder(FilePath,MapHistoryPath);
}
}
#endif