Files
envi-code/SourceCode/Code2026/tg_shs/shs_public/ShxCheckDlg.cpp
T
gjm 164968b62e chore
把非utf8-bom编码的cpp/h文件改为 utf8 bom 编码, msvc识别utf8编码时,如果不是bom格式的,会使用当前cp_oem来解码.
2026-10-04 00:04:20 +08:00

698 lines
19 KiB
C++

// ShxCheckDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Twtdef.h"
#include "TAPI.h"
#include "ShxCheckDlg.h"
//#include "UIIADORXServer.h"
#include "Comdef.h"
#include "Wininet.h"
#include "IID.h"
#include "LiveUpdateDefine.h"
//#include "zip.h"
//#include "unzip.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define DOWNLOAD_SOURCE _T("121.52.210.106") //保存下载文件信息的数据库所在地址
#define DOWNLOAD_DATABASE _T("tangent") //保存下载文件信息的数据库名
#define DOWNLOAD_USER _T("sa") //保存下载文件信息的数据库用户名
#define DOWNLOAD_PWD _T("Tgtg2010") //保存下载文件信息的数据库密码
#define LOC_DATABASE _T("Tangent.db3") //本地数据库
// {EF5E73CE-F0EA-43A2-AE72-8DA45704C3F2}
const CLSID CLSID_IComDemo =
{ 0xef5e73ce, 0xf0ea, 0x43a2, { 0xae, 0x72, 0x8d, 0xa4, 0x57, 0x4, 0xc3, 0xf2 } };
//************************************************************************************
// 字体检查
//************************************************************************************
/*
//将一个Zip解压出来,返回sWmfFile;
BOOL Zip2Wmf(LPCTSTR pszZipFile,CString& sWmfFile)
{
CString sZipFile = pszZipFile;
sZipFile.MakeLower();
if (sWmfFile.IsEmpty())
{
sWmfFile = sZipFile;
}
sWmfFile.MakeLower();
sWmfFile.Replace(_T(".zip"),_T(".wmf"));
CFileFind fileFind;
BOOL bDel = FALSE;
if (fileFind.FindFile(sWmfFile))
{
bDel = DeleteFile(sWmfFile);
}
try
{
#if ADS < 17
CUnzip oUnzip(sZipFile);
oUnzip.SwapSize(sWmfFile);
#else
char* pszZipFile = new char[sZipFile.GetLength() * 2 + 1];
wcstombs(pszZipFile,sZipFile,sZipFile.GetLength() * 2 + 1);
char* pszWmfFile = new char[sWmfFile.GetLength() * 2 + 1];
wcstombs(pszWmfFile,sWmfFile,sWmfFile.GetLength() * 2 + 1);
CUnzip oUnzip(pszZipFile);
oUnzip.SwapSize(pszWmfFile);
delete []pszWmfFile;
delete []pszZipFile;
#endif
}
catch(CZipException e)
{
printf( "problem : %s\n", e.GetString() );
return FALSE;
}
return TRUE;
}
*/
void DownloadShx(CString csShx)
{
CString sFileName = _T(""), sAddress = _T(""), sSQL = _T("");
BOOL bSuc = FALSE;
// 调用COM组件,返回下载结果
IUnknown *pUnk = NULL;
HRESULT hr = ::GetActiveObject(IID_IArxComLiveUpdateDlg, NULL, &pUnk);
if (FAILED(hr))
{
hr = ::CoCreateInstance(CLSID_IArxDownloadInfo, NULL, CLSCTX_LOCAL_SERVER, IID_IUnknown, (LPVOID*)&pUnk);
if (FAILED(hr))
{
_com_error err(hr);
AfxMessageBox(err.ErrorMessage());
return;
}
}
IDispatch FAR* pDispatch = NULL;
hr = pUnk->QueryInterface(IID_IDispatch,(LPVOID*) &pDispatch);
if (FAILED(hr))
{
_com_error err(hr);
AfxMessageBox(err.ErrorMessage());
return ;
}
pUnk->Release();
OLECHAR FAR* szFuncName = L"SendDownInfo";
DISPID dispid;
hr = pDispatch->GetIDsOfNames(IID_NULL, &szFuncName, 1, LOCALE_USER_DEFAULT, &dispid);
if (FAILED(hr))
{
_com_error err(hr);
AfxMessageBox(err.ErrorMessage());
pDispatch->Release();
return ;
}
VARIANTARG vararg[2];
VariantInit(&vararg[0]);
VariantInit(&vararg[1]);
DISPPARAMS dispparams;
DownloadInfo di;
sFileName = csShx + _T(".shx");
sAddress = _T("http://121.52.210.106/downloads/shx/") + sFileName;
// CString csNewFile = g_csSysPath + csShx + _T(".shx");
// CString csZipFile = g_csSysPath + csShx + _T(".zip");
dispparams.rgvarg = vararg;
ZeroMemory(&di, sizeof(di));
di.nSpec = 1;//专业
_tcscpy(di.szDownloadURL, sAddress);
_tcscpy(di.szSavePath, g_csSysPath);
_tcscpy(di.szSaveOnlyFileName, sFileName);
_tcscpy(di.szUsername, _T(""));
_tcscpy(di.szPassword, _T(""));
di.bForceDownload = FALSE;
di.bCloseDownloadWnd = TRUE;
di.bShowDownloadWnd = TRUE;
di.nThreadCount = 4;
di.bCallComOnEnd = FALSE;
di.bUseDefaultCLSID = TRUE;
CString sDownloadInfo = _T(""), sCLSID = _T("");
GetDownloadParam(&di, sDownloadInfo, sCLSID);
dispparams.rgvarg[0].vt = VT_BSTR; //
dispparams.rgvarg[0].bstrVal = sDownloadInfo.AllocSysString();
dispparams.rgvarg[1].vt = VT_BSTR; //
dispparams.rgvarg[1].bstrVal = sCLSID.AllocSysString();
VARIANT varRes;
VariantInit(&varRes);
varRes.vt = VT_I4;
varRes.lVal = 0;
dispparams.rgdispidNamedArgs = NULL;
dispparams.cArgs = 2;
dispparams.cNamedArgs = 0;
//5. 调用组件方法
hr = pDispatch->Invoke(
dispid,
IID_NULL,
LOCALE_USER_DEFAULT,
DISPATCH_METHOD,
&dispparams,
&varRes, NULL, NULL);
if (FAILED(hr))
{
_com_error err(hr);
AfxMessageBox(err.ErrorMessage());
pDispatch->Release();
return ;
}
pDispatch->Release();
//解压缩
//
// HZIP hz = OpenZip(csZipFile, 0);
// ZIPENTRY ze;
// CString csFileName;
// GetZipItem(hz, -1, &ze);
// int nNumitems = ze.index;
// for (int zi = 0; zi < nNumitems; zi++)
// {
// ZIPENTRY ze;
// GetZipItem(hz, zi, &ze);
// csFileName.Format(_T("%s\\%s"), g_csSysPath, ze.name); // 解压后的路径
// UnzipItem(hz, zi, csFileName);
// }
// CloseZip(hz);
// return;
}
BOOL GetTextStyleFileName(AcDbTextStyleTableRecord *pRec, CString &strFontName, CString &csEngFontName)
{
BOOL bRet = TRUE;
if(pRec == NULL)
return FALSE;
const TCHAR* pShxName = NULL;
pRec->fileName(pShxName);
const TCHAR* pbigFontFileName = NULL;
pRec->bigFontFileName(pbigFontFileName);
if(pbigFontFileName)
strFontName = pbigFontFileName;
else
strFontName = _T("");
csEngFontName = pShxName;
if(csEngFontName != _T(""))
{//先检查英文字体文件有没有缺少
int idx = csEngFontName.Find(_T("."));
if(idx > -1)
{//发现了,都去掉.后缀
csEngFontName = csEngFontName.Left(idx);
}
}
if(strFontName != _T(""))
{//再检查中文字体文件有没有缺少
int idx = strFontName.Find(_T("."));// 特别讨厌,有点带.SHX 有的不带
if(idx > -1)
{//发现了,都去掉.后缀
strFontName = strFontName.Left(idx);
}
}
return TRUE;
}
void CheckTextStyle()
{
CAcModuleResourceOverride vTemp;
CShxCheckDlg dlg;
//1.0 首先遍历有多少SHX文件
XStringList lstEngShx, lstChnShx, lstTarget;
XStringList lstSearchPath;
GetSearchPath(lstSearchPath);
for(int i=0; i<lstSearchPath.Length(); i++)
{
XString sFilter(256);
sFilter << lstSearchPath[i] << _T("*.SHX");
XStringList lstFilesFound;
struct _tfinddata_t fb;
//LONG_PTR hand;
intptr_t hand;
hand=_tfindfirst(sFilter,&fb);
if(hand==-1L) continue;
lstFilesFound.Append(_tcsupr(tstrnew(fb.name)));
while (!_tfindnext(hand,&fb))
{
lstFilesFound.Append(_tcsupr(tstrnew(fb.name)));
}
_findclose(hand);
for(int n = 0; n < lstFilesFound.Length(); n++)
{
XString sFontFile(256);
sFontFile << lstSearchPath[i] << lstFilesFound[n];
char sBuf[18];
FILE *fp=fopen(_AS(sFontFile), "r");
if(!fp)
return;
fgets(sBuf, 17, fp);
fclose(fp);
sBuf[17]=0;
if(stricmp(&sBuf[11], "shape")==0 || stricmp(&sBuf[11], "unifo")==0)
{
if(lstEngShx.Index(lstFilesFound[n], 0)<0) // english font
lstEngShx.Append(tstrnew(lstFilesFound[n]));
}
else if(stricmp(&sBuf[11], "bigfo")==0)
{
if(lstChnShx.Index(lstFilesFound[n], 0)<0) // bigfont
lstChnShx.Append(tstrnew(lstFilesFound[n]));
}
}
}
//去掉后面的.shx
for(i = 0; i < lstEngShx.Length(); i++)
{
TCHAR *p = _tcschr(lstEngShx[i], _T('.'));
if(p) *p = 0;
}
for(i = 0; i < lstChnShx.Length(); i++)
{
TCHAR *p = _tcschr(lstChnShx[i], _T('.'));
if(p) *p = 0;
}
lstChnShx.QuickSort(_tcscmp);
lstEngShx.QuickSort(_tcscmp);
//2.0 一一检查哪些文字样式需要修正
XSymbolList lstStyle(_T("STYLE"));
for (int m =0; m < lstStyle.Length(); m++) {
CString csStyle = lstStyle[m];
AcDbObjectId idTextStyle = GetTextStyleId(csStyle);
if(csStyle == _T("")){
OPENOBJ_BEGIN(idTextStyle, AcDb::kForWrite, AcDbTextStyleTableRecord, pStyle);
if(pStyle){
pStyle->erase();
}
OPENOBJ_END();
adsout << _T("已经删除非法文字样式\n");
}
else{
OPENOBJ_BEGIN(idTextStyle, AcDb::kForRead, AcDbTextStyleTableRecord, pRec);
if(pRec){
TCHAR* pfileName = NULL;
#if ADS > 20
Adesk::Boolean bold, italic;
Charset charset;
Autodesk::AutoCAD::PAL::FontUtils::FontPitch pitch;
Autodesk::AutoCAD::PAL::FontUtils::FontFamily family;
pRec->font(pfileName, bold, italic, charset, pitch, family);
#else
BOOL bold, italic; int i, j;
pRec->font(pfileName, bold, italic, i, j);
#endif
if(pfileName && _tcscmp(pfileName,_T("")) != 0) { // Windows 字体
}
else { // AutoCAD 字体
CString strFontName, csEngFontName;
GetTextStyleFileName(pRec, strFontName, csEngFontName);
// const TCHAR* pShxName = NULL;
// pRec->fileName(pShxName);
// const TCHAR* pbigFontFileName = NULL;
// pRec->bigFontFileName(pbigFontFileName);
// CString strFontName, csEngFontName;
// if(pbigFontFileName)
// strFontName = pbigFontFileName;
// else
// strFontName = _T("");
// csEngFontName = pShxName;
if(csEngFontName != _T(""))
{//先检查英文字体文件有没有缺少
// int idx = csEngFontName.Find(_T("."));
// if(idx > -1)
// {//发现了,都去掉.后缀
// csEngFontName = csEngFontName.Left(idx);
// }
if(lstEngShx.Index(csEngFontName, 0)<0)
{ //需要改,都记下来
if(lstTarget.Index(csStyle, 0) < 0)
lstTarget.Append(tstrnew(csStyle));
if(dlg.m_lstTargetEngShx.Index(csEngFontName, 0)<0)
{
dlg.m_lstTargetEngShx.Append(tstrnew(csEngFontName));
}
}
}
if(strFontName != _T(""))
{//再检查中文字体文件有没有缺少
// int idx = strFontName.Find(_T("."));// 特别讨厌,有点带.SHX 有的不带
// if(idx > -1)
// {//发现了,都去掉.后缀
// strFontName = strFontName.Left(idx);
// }
if(lstChnShx.Index(strFontName, 0)<0)// bigfont
{ //需要改,都记下来
if(lstTarget.Index(csStyle, 0) < 0)
lstTarget.Append(tstrnew(csStyle));
if(dlg.m_lstTargetShx.Index(strFontName, 0)<0)// bigfont
{
dlg.m_lstTargetShx.Append(tstrnew(strFontName));
}
}
}
}
}
OPENOBJ_END();
}
}
int len = lstTarget.Length();
int len2 = dlg.m_lstTargetShx.Length();
if(len < 1)
return;
DWORD flags;
// dlg.m_bCon = ::InternetCheckConnection(_T("http://www.sina.com.cn"),FLAG_ICC_FORCE_CONNECTION ,0);
dlg.m_bCon = InternetGetConnectedState(&flags, NULL);
CUIISQLiteServer adoSrv;
if (dlg.m_bCon)
{
//1. 从数据库中获取该专业的可供下载的文件信息并下载用户选择的文件
//连接数据库
/*CString sConnect = _T("");
sConnect.Format(_T("Provider=sqloledb;Data Source=%s;")
_T("Initial Catalog=%s;User Id=%s;Password=%s;"),
DOWNLOAD_SOURCE, DOWNLOAD_DATABASE,DOWNLOAD_USER,DOWNLOAD_PWD);*/
XString xsPath;
if (!FindFile(LOC_DATABASE, xsPath))
{
acutPrintf(_T("没有找到数据库文件!"));
return;
}
CString sConnect = xsPath;
BOOL bConnect = adoSrv.InitADOServer(sConnect);
//#if _WIN64
// BOOL bConnect = adoSrv.InitADOServer(sConnect);
//#else
// BOOL bConnect = adoSrv.InitADOServer(sConnect,true);
//#endif
if(!bConnect)
{
acutPrintf(_T("数据库连接失败!"));
return;
}
CString sSQL = _T("select shx_Name from tg_shx where shx_Isupload = 0"); //
adoSrv.GetSelData(sSQL, dlg.m_arValue);
}
if(dlg.DoModal() != IDOK)
return;
if(dlg.m_iMode == 0) //替换
{
for(i=0;i<len;i++){
CString csStyleTmp = lstTarget[i];
AcDbObjectId idTextStyle = GetTextStyleId(csStyleTmp);
OPENOBJ_BEGIN(idTextStyle, AcDb::kForWrite, AcDbTextStyleTableRecord, pRec);
if(pRec){
CString strFontName, csEngFontName;
GetTextStyleFileName(pRec, strFontName, csEngFontName);
if(dlg.m_lstTargetShx.Index(strFontName, 0) > -1)
{
pRec->setBigFontFileName(dlg.m_csShxReplace);
CString csPrompt;
csPrompt.Format(_T("\n已经成功将%s样式的中文字体改为%s"), csStyleTmp, dlg.m_csShxReplace);
adsout << (const TCHAR*)csPrompt;
}
if(dlg.m_lstTargetEngShx.Index(csEngFontName, 0) > -1)
{
pRec->setFileName(dlg.m_csEngReplace);
CString csPrompt;
csPrompt.Format(_T("\n已经成功将%s样式的英文字体改为%s"), csStyleTmp, dlg.m_csEngReplace);
adsout << (const TCHAR*)csPrompt;
}
}
OPENOBJ_END();
}
}
else if(dlg.m_iMode == 1 && dlg.m_bCon)//下载
{
adoSrv.ExecuteSQL(_T("Begin Transaction"));
for(int t=0;t<len2;t++)
{
CString csStyleTmp = dlg.m_lstTargetShx[t];
BOOL bDown = FALSE;
for (int k = 0; k < dlg.m_arValue.GetSize(); k++)
{
CString csShx;
csShx = dlg.m_arValue.GetAt(k);
// 特别讨厌,有点带.SHX 有的不带
int idx2 = csShx.Find(_T("."));
if(idx2 > -1)
{//发现了,都去掉.后缀
csShx = csShx.Left(idx2);
}
if(csShx.CompareNoCase(csStyleTmp) == 0)
{//找到所缺的字体文件,开始下载
DownloadShx(csShx);
CString sSQL, sVal;
sSQL.Format(_T("select shx_Count from tg_shx where shx_Name = '%s'"), csShx);
adoSrv.GetSelData(sSQL, sVal);
int iCount = _ttoi(sVal);
iCount++;
sSQL.Format(_T("update tg_shx set shx_Count = %d where shx_Name = '%s'"), iCount, csShx);
adoSrv.ExecuteSQL(sSQL);
bDown = TRUE;
break;
}
}
CString csPrompt;
if(bDown)
{
csPrompt.Format(_T("\n已经成功下载您所缺的字体文件:%s.shx! 请REGEN更新本图。"), csStyleTmp);
adsout << (const TCHAR*)csPrompt;
}
else
{
CString csVal;
CString sSQL;
sSQL.Format(_T("select shx_Count from tg_shx where shx_Isupload = 1 and shx_Name = '%s'"), csStyleTmp); //
adoSrv.GetSelData(sSQL, csVal);
if(csVal != _T(""))
{
int iCount = _ttoi(csVal);
iCount++;
sSQL.Format(_T("update tg_shx set shx_Count = %d where shx_Name = '%s'"), iCount, csStyleTmp);
adoSrv.ExecuteSQL(sSQL);
}
else
{
sSQL.Format(_T("insert into tg_shx (shx_Name, shx_Isupload, shx_Address, shx_Remark, shx_Count) values ('%s', %d, '%s', '%s', %d)"), csStyleTmp, 1, _T(""), _T(""), 0);
adoSrv.ExecuteSQL(sSQL);
}
csPrompt.Format(_T("\n对不起,上华字体库暂时无%s.shx字体!"), csStyleTmp);
adsout << (const TCHAR*)csPrompt;
}
}
adoSrv.ExecuteSQL(_T("Commit Transaction"));
return;
}
}
/////////////////////////////////////////////////////////////////////////////
// CShxCheckDlg dialog
CShxCheckDlg::CShxCheckDlg(CWnd* pParent /*=NULL*/)
: CDialog(CShxCheckDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CShxCheckDlg)
m_csShxReplace = _T("");
m_iMode = 1;
m_csEngReplace = _T("");
//}}AFX_DATA_INIT
m_bCon = FALSE;
}
void CShxCheckDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CShxCheckDlg)
DDX_Control(pDX, IDC_COMBO_ENG, m_comboEng);
DDX_Control(pDX, IDC_LIST_TG, m_listTg);
DDX_Control(pDX, IDC_COMBO_SHX, m_comboChn);
DDX_Control(pDX, IDC_LIST_SHX, m_listShx);
DDX_CBString(pDX, IDC_COMBO_SHX, m_csShxReplace);
DDX_Radio(pDX, IDC_RADIO_REPLACE, m_iMode);
DDX_CBString(pDX, IDC_COMBO_ENG, m_csEngReplace);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CShxCheckDlg, CDialog)
//{{AFX_MSG_MAP(CShxCheckDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CShxCheckDlg message handlers
BOOL CShxCheckDlg::OnInitDialog()
{
CDialog::OnInitDialog();
LoadFontList();
for(int i=0;i<m_lstChnShx.Length();i++)
{
m_comboChn.AddString(m_lstChnShx[i]);
}
for(i=0;i<m_lstEngShx.Length();i++)
{
m_comboEng.AddString(m_lstEngShx[i]);
}
for(int m=0;m<m_lstTargetShx.Length();m++)
{
CString csStyleTmp = m_lstTargetShx[m];
m_listShx.AddString(csStyleTmp);
}
for(m=0;m<m_lstTargetEngShx.Length();m++)
{
CString csStyleTmp = m_lstTargetEngShx[m];
m_listShx.AddString(csStyleTmp);
}
for (int k = 0; k < m_arValue.GetSize(); k++)
{
CString csShx;
csShx = m_arValue.GetAt(k);
m_listTg.AddString(csShx);
}
CWnd *pWnd1 = GetDlgItem(IDC_COMBO_SHX);
CWnd *pWnd2 = GetDlgItem(IDC_COMBO_ENG);
if(m_lstTargetShx.Length() < 1)
pWnd1->EnableWindow(FALSE);
if(m_lstTargetEngShx.Length() < 1)
pWnd2->EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CShxCheckDlg::LoadFontList() // AutoCAD and Windows 字体的导入
{
m_lstChnShx.Reset();
m_lstEngShx.Reset();
XStringList lstSearchPath;
GetSearchPath(lstSearchPath);
for(int i=0; i<lstSearchPath.Length(); i++)
{
XString sFilter(256);
sFilter << lstSearchPath[i] << _T("*.SHX");
XStringList lstFilesFound;
struct _tfinddata_t fb;
//long hand;
intptr_t hand;
hand=_tfindfirst(sFilter,&fb);
if(hand==-1L) continue;
lstFilesFound.Append(_tcsupr(tstrnew(fb.name)));
while (!_tfindnext(hand,&fb))
{
lstFilesFound.Append(_tcsupr(tstrnew(fb.name)));
}
_findclose(hand);
for(int n = 0; n < lstFilesFound.Length(); n++)
{
XString sFontFile(256);
sFontFile << lstSearchPath[i] << lstFilesFound[n];
char sBuf[18];
FILE *fp=fopen(_AS(sFontFile), "r");
if(!fp)
return;
fgets(sBuf, 17, fp);
fclose(fp);
//ifstream in(sFontFile, ios::binary);
//if(!in) continue;
//TCHAR sBuf[17];
//in.read(sBuf, 16);
sBuf[17]=0;
if(stricmp(&sBuf[11], "shape")==0 || stricmp(&sBuf[11], "unifo")==0)
{
if(m_lstEngShx.Index(lstFilesFound[n], 0)<0) // english font
m_lstEngShx.Append(tstrnew(lstFilesFound[n]));
}
else if(stricmp(&sBuf[11], "bigfo")==0)
{
if(m_lstChnShx.Index(lstFilesFound[n], 0)<0) // bigfont
m_lstChnShx.Append(tstrnew(lstFilesFound[n]));
}
}
}
//去掉后面的.shx
for(i = 0; i < m_lstEngShx.Length(); i++)
{
TCHAR *p = _tcschr(m_lstEngShx[i], _T('.'));
if(p) *p = 0;
}
for(i = 0; i < m_lstChnShx.Length(); i++)
{
TCHAR *p = _tcschr(m_lstChnShx[i], _T('.'));
if(p) *p = 0;
}
m_lstChnShx.QuickSort(_tcscmp);
m_lstEngShx.QuickSort(_tcscmp);
g_lstWinfont.QuickSort(_tcscmp);
}