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.
425 lines
8.4 KiB
C++
425 lines
8.4 KiB
C++
#include "stdafx.h"
|
|
#include "ImageCut.h"
|
|
#include "DrawOperator/FileUtility.h"
|
|
|
|
static LPCTSTR CopyStr(LPCTSTR sourceStr)
|
|
{
|
|
size_t len = strlen(sourceStr);
|
|
if (len <= 0)
|
|
return NULL;
|
|
|
|
len++;
|
|
LPCTSTR dest = (LPCTSTR)malloc(len);
|
|
memset((void *)dest, 0, len);
|
|
memcpy((void *)dest, sourceStr, len-1);
|
|
return dest;
|
|
}
|
|
|
|
CutImageInfo * CutImageInfo::Copy()
|
|
{
|
|
CutImageInfo * info = new CutImageInfo();
|
|
info->fileNamesSelected = CopyStr(fileNamesSelected);
|
|
info->kindOfoutputDirectory = kindOfoutputDirectory;
|
|
info->outputDirectorySpecified = CopyStr(outputDirectorySpecified);
|
|
info->rangeCurveFileName = CopyStr(rangeCurveFileName);
|
|
|
|
return info;
|
|
}
|
|
|
|
ImageCut::ImageCut()
|
|
:m_ciInfo(0),
|
|
m_stopCutImage(false),
|
|
m_indexOfFileByCut(-1)
|
|
{
|
|
}
|
|
|
|
void ImageCut::SetCutImageInfo(CutImageInfo * ciInfo)
|
|
{
|
|
m_ciInfo = ciInfo;
|
|
//解析出文件列表
|
|
GetFileNamesByCuted();
|
|
}
|
|
|
|
int ImageCut::Execute()
|
|
{
|
|
m_stopCutImage = false;
|
|
m_indexOfFileByCut = -1;
|
|
if (m_ciInfo == 0)
|
|
{
|
|
//m_indexOfFileByCut = -1;
|
|
return -1;
|
|
}
|
|
|
|
//解析出文件列表
|
|
//GetFileNamesByCuted();
|
|
|
|
int total = m_fileNamesSelected.GetCount();//(int)pSheet->pageFileSelect.m_listFile.GetCount()
|
|
if (total == 0)
|
|
{
|
|
//::AfxMessageBox("Please select files");
|
|
m_indexOfFileByCut = -2;
|
|
return -2;
|
|
}
|
|
|
|
int count = 0;
|
|
CXy range_xy;
|
|
if (range_xy.ReadWithExtension(m_ciInfo->rangeCurveFileName) == 0)
|
|
{
|
|
//::AfxMessageBox("Range File ERROR!");
|
|
m_indexOfFileByCut = -3;
|
|
return -3;
|
|
}
|
|
|
|
POSITION pos;
|
|
pos = range_xy.FindFirstElement(DOUBLEFOX_CURVE);
|
|
if (pos == NULL)
|
|
pos = range_xy.FindFirstElement(DOUBLEFOX_CRECT);
|
|
else if (pos == NULL)
|
|
pos = range_xy.FindFirstElement(DOUBLEFOX_ELLIPSE);
|
|
else if (pos == NULL)
|
|
pos = range_xy.FindFirstElement(DOUBLEFOX_CIRCLE);
|
|
else if (pos == NULL)
|
|
{
|
|
m_indexOfFileByCut = -4;
|
|
return -4;
|
|
}
|
|
|
|
BOOL bDeleteCurve = FALSE;
|
|
CCurveEx* pRgn = GetRangeCurve(&range_xy, pos, bDeleteCurve);
|
|
if (pRgn == NULL)
|
|
{
|
|
m_indexOfFileByCut = -5;
|
|
return -5;
|
|
}
|
|
|
|
//bool bCutOut = TRUE;
|
|
//EnableOther(FALSE);
|
|
|
|
//CWnd* pWndInfo = GetDlgItem(IDC_STATIC_PROGRESS);
|
|
CString str;
|
|
CString name;
|
|
|
|
m_indexOfFileByCut = -1;
|
|
int countOfFN = m_fileNamesSelected.GetCount();
|
|
pos = m_fileNamesSelected.GetHeadPosition();
|
|
//m_progress.SetRange32(1, count);
|
|
while (pos)
|
|
{
|
|
m_indexOfFileByCut++;
|
|
count++;
|
|
//m_progress.SetPos(count);
|
|
//AfxGetPublicFunction()->PeekMessageLoop();
|
|
if (m_stopCutImage)
|
|
{
|
|
m_stopCutImage = false;
|
|
m_indexOfFileByCut = -1000;
|
|
return 0;
|
|
}
|
|
|
|
name = m_fileNamesSelected.GetNext(pos);
|
|
str.Format("%ld/%ld", count, total);
|
|
//if (pWndInfo)pWndInfo->SetWindowText(str);
|
|
|
|
//CutOut(pRgn, strPath, name, nOutMode);
|
|
CutOut(pRgn, name, m_ciInfo->kindOfoutputDirectory);
|
|
|
|
//AfxGetPublicFunction()->PeekMessageLoop();
|
|
}
|
|
if (bDeleteCurve)
|
|
delete pRgn;
|
|
//::AfxMessageBox("Finished");
|
|
|
|
//EnableOther(TRUE);
|
|
m_indexOfFileByCut = 1000; //标识切割文件完毕
|
|
return count > 0 ? 1 : -6;
|
|
}
|
|
|
|
void ImageCut::StopCutImage(bool b)
|
|
{
|
|
m_stopCutImage = b;
|
|
}
|
|
|
|
int ImageCut::GetIndexOfFileByCut()
|
|
{
|
|
return m_indexOfFileByCut;
|
|
}
|
|
int ImageCut::CutOutFileByLine(BYTE * buffWellPath, int bufLen, CString sourceFile, CString destFile)
|
|
{
|
|
CXy range_xy;
|
|
CMemFile mf(buffWellPath, bufLen);
|
|
if (range_xy.DFD_Read2(mf) == 0) {
|
|
return -3;
|
|
}
|
|
return CutOutFile(range_xy, sourceFile, destFile);
|
|
}
|
|
/// <summary>
|
|
/// Cuts the out file.
|
|
/// </summary>
|
|
/// <param name="rangeFile">The range file.</param>
|
|
/// <param name="sourceFile">The source file.</param>
|
|
/// <param name="destFile">The dest file.</param>
|
|
/// <returns></returns>
|
|
int ImageCut::CutOutFile(CString rangeFile, CString sourceFile, CString destFile)
|
|
{
|
|
int count = 0;
|
|
// 读取范围文件
|
|
CXy range_xy;
|
|
if (!OpenFile(range_xy, rangeFile))
|
|
{
|
|
return -3;
|
|
}
|
|
|
|
return CutOutFile(range_xy, sourceFile, destFile);
|
|
}
|
|
|
|
int ImageCut::CutOutFile(CXy &range_xy, CString &sourceFile, CString &destFile)
|
|
{
|
|
POSITION pos;
|
|
pos = range_xy.FindFirstElement(DOUBLEFOX_CURVE);
|
|
if (pos == NULL)
|
|
pos = range_xy.FindFirstElement(DOUBLEFOX_CRECT);
|
|
else if (pos == NULL)
|
|
pos = range_xy.FindFirstElement(DOUBLEFOX_ELLIPSE);
|
|
else if (pos == NULL)
|
|
pos = range_xy.FindFirstElement(DOUBLEFOX_CIRCLE);
|
|
else if (pos == NULL)
|
|
{
|
|
return -4;
|
|
}
|
|
|
|
BOOL bDeleteCurve = FALSE;
|
|
CCurveEx* pRgn = GetRangeCurve(&range_xy, pos, bDeleteCurve);
|
|
if (pRgn == NULL)
|
|
{
|
|
return -5;
|
|
}
|
|
// 读取源文件
|
|
CXy* pxySource = new CXy();
|
|
pxySource->m_bRealTimeDraw = false;
|
|
if (!OpenFile(*pxySource, sourceFile))
|
|
{
|
|
delete pxySource;
|
|
if (bDeleteCurve)
|
|
delete pRgn;
|
|
return -6;
|
|
}
|
|
|
|
CPointList list;
|
|
pRgn->GetPoint(list);
|
|
|
|
CCurveEx curveCut;
|
|
if (pRgn->x[pRgn->num] != pRgn->x[0] || pRgn->y[pRgn->num] != pRgn->y[0])
|
|
{
|
|
dfPoint ptEnd;
|
|
ptEnd.x0 = pRgn->x[0];
|
|
ptEnd.y0 = pRgn->y[0];
|
|
list.AddTail(ptEnd);
|
|
|
|
curveCut.SetPoints(list, 2);
|
|
}
|
|
else {
|
|
curveCut.SetPoints(list, 2);
|
|
}
|
|
if (bDeleteCurve)
|
|
delete pRgn;
|
|
|
|
// 执行裁切
|
|
CClientDC dc(NULL);
|
|
CXyDC xydc;
|
|
xydc.Create(&dc);
|
|
|
|
CCutOut co;
|
|
co.SetXY(pxySource);
|
|
co.m_dMiniCurveSmoothStep = xydc.GetMiniSmoothStep();
|
|
co.CutOut(&curveCut);
|
|
co.DeleteElement(co.m_plDel);
|
|
|
|
// 保存裁切结果
|
|
pxySource->SaveAsWithExtension(destFile);
|
|
|
|
delete pxySource;
|
|
|
|
return 1;
|
|
}
|
|
|
|
int ImageCut::CutOut(CCurveEx* pRangeCurve, CString strFileName, int nOutMode)
|
|
{
|
|
CSplitPath sp(strFileName);
|
|
CString output;
|
|
|
|
switch (nOutMode)
|
|
{
|
|
case 0:
|
|
output = sp.GetPath() + sp.GetName() + _T(".dfb");
|
|
break;
|
|
case 1:
|
|
{
|
|
if (m_ciInfo->outputDirectorySpecified == NULL)
|
|
return -1;
|
|
|
|
CString strPath(m_ciInfo->outputDirectorySpecified);
|
|
if (strPath.IsEmpty())
|
|
return -1;
|
|
|
|
if (strPath[strPath.GetLength() - 1] == '\\')
|
|
output = strPath + sp.GetName() + _T(".dfb");
|
|
else
|
|
output = strPath + _T("\\") + sp.GetName() + _T(".dfb");
|
|
break;
|
|
}
|
|
default:
|
|
return -1;
|
|
}
|
|
|
|
// AfxGetPublicFunction()->__CurrentSaveFilePath = output;
|
|
AfxGetPublicFunction()->InitSaveCounter();
|
|
|
|
CXy* pxy = new CXy();
|
|
//AfxGetPublicFunction()->SetProgress(&m_one);
|
|
|
|
int type = pxy->ReadWithExtension(strFileName);
|
|
if (type > 0)
|
|
{
|
|
//CClientDC dc(this);
|
|
CClientDC dc(NULL);
|
|
CXyDC xydc;
|
|
xydc.Create(&dc);
|
|
|
|
CCutOut co;
|
|
co.SetXY(pxy);
|
|
co.m_dMiniCurveSmoothStep = xydc.GetMiniSmoothStep();
|
|
co.CutOut(pRangeCurve);
|
|
co.DeleteElement(co.m_plDel);
|
|
|
|
pxy->SaveAsWithExtension(output);
|
|
//switch(type)
|
|
//{
|
|
//case 1:
|
|
// break;
|
|
//case 2:
|
|
// break;
|
|
//case 3:
|
|
// break;
|
|
//}
|
|
}
|
|
delete pxy;
|
|
|
|
return 1;
|
|
}
|
|
|
|
bool ImageCut::OpenFile(CXy& xy, LPCTSTR filePath)
|
|
{
|
|
if (xy.ReadOtherWithExtension(filePath))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
CString path(filePath);
|
|
CString tempFile = PreprocessFile(path);
|
|
if (tempFile.IsEmpty())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
CFile file;
|
|
if (file.Open(tempFile, CFile::modeRead))
|
|
{
|
|
CArchive ar(&file, CArchive::load);
|
|
Serialize(xy, ar);
|
|
ar.Close();
|
|
file.Close();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void ImageCut::Serialize(CXy& xy, CArchive& ar)
|
|
{
|
|
CString name = ar.GetFile()->GetFileName();
|
|
CSplitPath splitPath(name);
|
|
CString ext = splitPath.GetExtension();
|
|
|
|
ext.MakeLower();
|
|
|
|
if (ext == ".kev" || ext == ".dfd" || ext == ".xyz")
|
|
{
|
|
xy.DFD_Read2(*ar.GetFile());
|
|
}
|
|
}
|
|
|
|
|
|
void ImageCut::SplitStr(CString strSrc, CString strGap, CStringList &strResult)
|
|
{
|
|
int nPos = strSrc.Find(strGap);
|
|
CString strLeft = _T("");
|
|
|
|
while (0 <= nPos)
|
|
{
|
|
strLeft = strSrc.Left(nPos);
|
|
if (!strLeft.IsEmpty())
|
|
{
|
|
strResult.AddTail(strLeft);
|
|
}
|
|
|
|
strSrc = strSrc.Right(strSrc.GetLength() - nPos - strGap.GetLength());
|
|
nPos = strSrc.Find(strGap);
|
|
}
|
|
|
|
if (!strSrc.IsEmpty())
|
|
{
|
|
strResult.AddTail(strSrc);
|
|
}
|
|
}
|
|
|
|
CCurveEx* ImageCut::GetRangeCurve(CXy* pxy, POSITION pos, BOOL& bDeleteCurve)
|
|
{
|
|
if (pxy == NULL || pos == NULL) return NULL;
|
|
|
|
COne* pOne = pxy->GetAt(pos);
|
|
bDeleteCurve = FALSE;
|
|
CCurveEx* pRgn = NULL;
|
|
switch (pOne->GetType())
|
|
{
|
|
case DOUBLEFOX_CURVE:
|
|
pRgn = (CCurveEx*)pOne->GetValue();
|
|
break;
|
|
case DOUBLEFOX_ARC:
|
|
pRgn = ((CArc*)pOne->GetValue())->GetCurve();
|
|
break;
|
|
case DOUBLEFOX_CRECT:
|
|
pRgn = new CCurveEx;
|
|
((CCurveRect*)pOne->GetValue())->GetCurve(*pRgn);
|
|
bDeleteCurve = TRUE;
|
|
break;
|
|
case DOUBLEFOX_CIRCLE:
|
|
case DOUBLEFOX_ELLIPSE:
|
|
pRgn = ((CEllipse*)pOne->GetValue())->ToCurve(2.0);
|
|
bDeleteCurve = TRUE;
|
|
break;
|
|
default:
|
|
return NULL;
|
|
}
|
|
return pRgn;
|
|
}
|
|
|
|
bool ImageCut::GetFileNamesByCuted()
|
|
{
|
|
CStringList strList;
|
|
if (m_ciInfo == NULL)
|
|
return false;
|
|
|
|
if (m_ciInfo->fileNamesSelected == NULL ||
|
|
m_ciInfo->rangeCurveFileName == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
CString fileNames(m_ciInfo->fileNamesSelected);
|
|
SplitStr(fileNames, ";", m_fileNamesSelected);
|
|
if (m_fileNamesSelected.IsEmpty())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
} |