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

1033 lines
25 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "stdafx.h"
#include "model3dlibdlg.h"
#include "coretools.h"
#include "KTModelLayoutXData.h"
#include "dbvisualstyle.h"
#include "KTCadUtility.h"
#include "aecdbx3dpartentity.h"
#ifndef ZH_SB_BIM_REC
#define ZH_SB_BIM_REC _T("MODEL_3D_BIM_REC")
#endif
#ifndef ZH_SB_BLK_NAME
#define ZH_SB_BLK_NAME _T("MODEL_3D_BLK_NAME")
#endif
#ifndef ZH_SB_BLK_EXT
#define ZH_SB_BLK_EXT _T("MODEL_3D_BLK_EXT")
#endif
#ifndef ZH_SB_FLAG
#define ZH_SB_FLAG _T("MODEL_3D_FLAG")
#endif
#ifndef ZH_SB_NAME
#define ZH_SB_NAME _T("MODEL_3D_NAME")
#endif
#ifndef ZH_SB_REF_FIELD
#define ZH_SB_REF_FIELD _T("MODEL_3D_REF_FIELD")
#endif
namespace
{
bool SaveBitmapToFile(CBitmap *bitmap, AcDbBlockTableRecord::PreviewIcon &icon)
{
CString sPath(FilePath::getSystemTemporaryFolder() + _T("\\a_b_cc__.bmp"));
CImage img;
img.Attach(*bitmap);
HRESULT hr = img.Save(sPath);
img.Detach();
if (!FAILED(hr) && _taccess(sPath, 0x0) == 0x0)
{
CFile fi;
if (fi.Open(sPath, CFile::modeRead))
{
icon.setLogicalLength(fi.GetLength());
fi.Read(icon.asArrayPtr(), fi.GetLength());
}
}
return !icon.isEmpty();
}
HBITMAP PreviewIcon2Bitmap(const AcDbBlockTableRecord::PreviewIcon &icon)
{
CString sPath(FilePath::getSystemTemporaryFolder() + _T("\\a_v_v.bmp"));
CFile file(sPath, CFile::modeReadWrite | CFile::modeCreate);
if (file.m_hFile)
{
file.Write((void *)icon.asArrayPtr(), icon.length());
file.Close();
return (HBITMAP)::LoadImage(0, sPath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
}
return NULL;
}
void ScreenCopy(const AcGePoint3d &wptTL, const AcGePoint3d &wptBR, AcDbBlockTableRecord::PreviewIcon &icon)
{
CView *pView = acedGetAcadDwgView();
if (!pView) return;
CDC *pDc = pView->GetDC();
if (!pDc) return;
CRect rect;
if (wptTL == wptBR)
pView->GetClientRect(rect);
else
rect.SetRect(CKTCadUtility::Wcs2Pixel(wptTL), CKTCadUtility::Wcs2Pixel(wptBR));
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(pDc, rect.Width(), rect.Height());
CDC memDc;
memDc.CreateCompatibleDC(pDc);
CBitmap *pOldBitmap = memDc.SelectObject(&bitmap);
memDc.BitBlt(0
, 0
, rect.Width()
, rect.Height()
, pDc
, rect.left
, rect.top
, SRCCOPY);
CMemFile file;
SaveBitmapToFile(&bitmap, icon);
memDc.SelectObject(pOldBitmap);
memDc.DeleteDC();
::ReleaseDC(pView->GetSafeHwnd(), pDc->GetSafeHdc());
}
void SetViewportDir(const AcGeVector3d &dir
, const AcGePoint3d &ptMax
, const AcGePoint3d &ptMin)
{
AcGePlane plan(AcGePoint3d::kOrigin, dir);
AcGePoint2d pt1 = ptMin.convert2d(plan);
AcGePoint2d pt2 = ptMax.convert2d(plan);
AcGePoint2d pt(CoreTools::MidPt(pt1, pt2));
AcDbViewTableRecord VTR;
VTR.setViewDirection(dir);
VTR.setCenterPoint(pt);
VTR.setWidth(abs(pt1.x - pt2.x));
VTR.setHeight(abs(pt1.y - pt2.y));
acedSetCurrentView(&VTR, NULL);
}
void SetVistualStyle(LPCTSTR pszVSName)
{
if (!pszVSName) return;
AcDbObjectId idVs(AcDbObjectId::kNull);
AcDbDatabase *pDb = acdbHostApplicationServices()->workingDatabase();
if (!pDb) return;
AcDbDictionary *pVsD = NULL;
if (pDb->getVisualStyleDictionary(pVsD, AcDb::kForRead) != Acad::eOk) return;
AcDbVisualStyle *pVs(NULL);
if (Acad::eOk == pVsD->getAt(pszVSName, (AcDbObject *&)pVs, AcDb::kForRead))
{
idVs = pVs->objectId();
pVs->close();
}
pVsD->close();
if (idVs.isValid())
acdbSetViewportVisualStyle(idVs);
}
std::shared_ptr<AcDbDatabase> getZhSbFileFromDb(CUIISQLiteServer &db, const int &fid)
{
CString sPath(FilePath::getSystemTemporaryFolder() + _T("\\temp_$_$_.dwg"));
if (_taccess(sPath, 0x0) == 0x0)
::DeleteFile(sPath);
CString sSQL(_T(""));
sSQL.Format(_T("select package from model_package where ID='%d'"), fid);
if (db.GetSelBlob(sSQL, sPath))
{
AcDbDatabase *p(new AcDbDatabase(false));
if (p->readDwgFile(sPath, AcDbDatabase::kTryForReadShare) == Acad::eOk)
return std::shared_ptr<AcDbDatabase>(p);
else
DELETE_PTR(p);
}
return std::shared_ptr<AcDbDatabase>();
}
bool readXDataFromZhSbFile(AcDbDatabase *pDb, AecDbObjXDataMap &xData)
{
if (!pDb) return false;
return KTModelLayoutXData::ReadModelNameObjXData(ZH_SB_BIM_REC, xData, pDb) == FALSE ? false : true;
}
bool writeXDataFromZhSbFile(AcDbDatabase *pDb, AecDbObjXDataMap &xData)
{
if (!pDb) return false;
if (KTModelLayoutXData::WriteModelNameObjXData(ZH_SB_BIM_REC, xData, pDb))
#if ARX==18
return pDb->saveAs(pDb->originalFileName(), true, AcDb::kDHL_CURRENT) == Acad::eOk;
#else
return pDb->saveAs(pDb->originalFileName(), true, AcDb::kDHL_1027) == Acad::eOk;
#endif
return false;
}
bool updateDwgPackage(CUIISQLiteServer &db, const int &fid, LPCTSTR pszPath)
{
if (!pszPath) return false;
CString sql(_T(""));
sql.Format(_T("update model_package set package=? where id='%d'"), fid);
return db.UpdateBlob(sql, pszPath) == 0 ? false : true;
}
bool addBimRecToZhSbFile(CUIISQLiteServer &db, const int &fid, const SH_BIM_REC &rec)
{
std::shared_ptr<AcDbDatabase> pDb = getZhSbFileFromDb(db, fid);
AecDbObjXDataMap xDataMap;
if (!readXDataFromZhSbFile(pDb.get(), xDataMap)) return false;
xDataMap.SetAt(rec.first, rec.second);
if (!writeXDataFromZhSbFile(pDb.get(), xDataMap)) return false;
return updateDwgPackage(db, fid, pDb->originalFileName());
}
bool delBimRecToZhSbFile(CUIISQLiteServer &db, const int &fid, LPCTSTR sKey)
{
std::shared_ptr<AcDbDatabase> pDb = getZhSbFileFromDb(db, fid);
AecDbObjXDataMap xDataMap;
if (!readXDataFromZhSbFile(pDb.get(), xDataMap)) return false;
xDataMap.RemoveAt(sKey);
if (!writeXDataFromZhSbFile(pDb.get(), xDataMap)) return false;
return updateDwgPackage(db, fid, pDb->originalFileName());
}
CString getZhSbNameFromDb(CUIISQLiteServer &db, const int &fid)
{
CString sSQL(_T(""));
sSQL.Format(_T("SELECT model_folder.[Folder], model_class.[Class], model_package.[Name]")
_T(" FROM")
_T(" model_class INNER JOIN model_folder")
_T(" ON model_folder.[ID] = model_class.[IDFolder]")
_T(" INNER JOIN model_package")
_T(" ON model_package.[IDClass] = model_class.[ID]")
_T(" WHERE model_package.[ID] = '%d'"), fid);
std::vector<std::vector<CString>> v;
db.GetSelDataEx(sSQL, v);
if (v.empty()) return _T("");
if (v[0].size() < 2) return _T("");
CString ss(_T(""));
ss.Format(_T("%s\\%s\\%s"), v[0][0], v[0][1], v[0][2]);
ss.Replace(_T('\\'), _T('-'));
return ss;
}
bool getBimsFromZhSbFile(AcDbDatabase *pDb, SH_BIM_BLOCK &bims)
{
if (!pDb) return false;
AecDbObjXDataMap xDataMap;
readXDataFromZhSbFile(pDb, xDataMap);
CString s;
AecDbObjXDataMap::CIt it(xDataMap.Begin());
for (; it != xDataMap.End(); ++it)
{
if (xDataMap.GetAt(it->first, s))
bims.push_back(std::make_pair(it->first, s));
}
return !bims.empty();
}
bool getBimsFromZhSbId(CUIISQLiteServer &db, const int &fid, SH_BIM_BLOCK &bims)
{
std::shared_ptr<AcDbDatabase> pDb = getZhSbFileFromDb(db, fid);
return getBimsFromZhSbFile(pDb.get(), bims);
}
void insertZHSB(CUIISQLiteServer &db, const int &fid)
{
CString sBlkName = getZhSbNameFromDb(db, fid);
if (sBlkName.IsEmpty()) return;
AcDbDatabase *pCurDb = acdbHostApplicationServices()->workingDatabase();
if (!pCurDb) return;
std::shared_ptr<AcDbDatabase> pDb = getZhSbFileFromDb(db, fid);
if (!pDb) return;
CString sFlag(_T("_Z_"));
AcDbObjectId blkId;
AcDbBlockTableRecordPointer pCurRcd(sBlkName, pCurDb, AcDb::kForRead);
if (pCurRcd.openStatus() != Acad::eOk)
{ // 不存在,插入
AecDbObjXDataMap xPipeInit;
// 如果当前图纸没有初始设置
if (!KTModelLayoutXData::ReadModelXData(KT_PIPEINIT, xPipeInit))
{
// 从 pDb 中读取,写入当前数据库
if (KTModelLayoutXData::ReadModelXData(KT_PIPEINIT, xPipeInit, pDb.get()))
KTModelLayoutXData::WriteModelXData(KT_PIPEINIT, xPipeInit);
}
AcDbBlockTableRecordPointer pNewRcd(ZH_SB_BLK_NAME, pDb.get(), AcDb::kForRead);
if (pNewRcd.openStatus() != Acad::eOk) return;
AcGePoint3d pt = pNewRcd->origin();
Adesk::Int16 nVal = acdbCurDwg()->useri4();
acdbCurDwg()->setUseri4(KEY_NOPROTECT);
AcDbDatabase *pTempDb(NULL);
if (pDb->wblock(pTempDb, pNewRcd->id()) == Acad::eOk)
{
if (pCurDb->insert(blkId, sBlkName, pTempDb) == Acad::eOk)
{
AecDbObjXDataMap xdata;
xdata.SetAt(sFlag, pt.z);
xdata.Write(blkId, sFlag);
}
}
DELETE_PTR(pTempDb);
acdbCurDwg()->setUseri4(nVal);
}
else
{ // 已经存在
blkId = pCurRcd->id();
pCurRcd->close();
}
if (!blkId.isValid()) return;
ads_real dElev(.0);
{
AecDbObjXDataMap xdata;
if (xdata.Read(blkId, sFlag))
xdata.GetAt(sFlag, dElev);
CString sPrompts;
sPrompts.Format(_T("\n输入高程<%.2fmm>:"), dElev);
acedGetDist(NULL, sPrompts, &dElev);
}
ads_point adsPtIns;
if (acedGetPoint(NULL, _T("\n选择插入点:"), adsPtIns) != RTNORM) return;
AcGePoint3d ptIns(asPnt3d(adsPtIns));
CoreTools::UCS2WCS(ptIns, ptIns);
ptIns.z = dElev;
// 做块
AcDbBlockReference *pRef(new AcDbBlockReference(ptIns, blkId));
CString sZHSBName = sBlkName.Right(sBlkName.GetLength() - 1 - sBlkName.ReverseFind(_T('-')));
SH_BIM_BLOCK bims;
getBimsFromZhSbFile(pDb.get(), bims);
CoreTools::setBimDatas(pRef, bims);
CoreTools::setBimData(pRef, KEY_NAME, sZHSBName);
// 为块打上“组合设备”的标记
AecDbObjXDataMap map;
map.SetAt(ZH_SB_FLAG, TRUE);
map.SetAt(ZH_SB_NAME, sZHSBName);
map.Write(pRef, ZH_SB_REF_FIELD);
CoreTools::AddToModelSpace(pRef);
}
}
BEGIN_MESSAGE_MAP(CModel3dLibDlg, CModelLibDlgBase)
END_MESSAGE_MAP()
CModel3dLibDlg::CModel3dLibDlg(LPCTSTR pszDir , LPCTSTR pszFile)
: CModelLibDlgBase(_T("模型库"), pszDir, pszFile)
, m_sTablePrefix(_T("MODEL"))
, m_sFileSuffix(_T(".shmod"))
, m_sRootName(_T("上华三维模型"))
{
}
BOOL CModel3dLibDlg::OnInitDialog()
{
CModelLibDlgBase::OnInitDialog();
CWnd *pWnd = GetDlgItem(IDC_INSERT);
if (pWnd)
pWnd->SetWindowText(_T("插入"));
return TRUE;
}
bool CModel3dLibDlg::initSqliteDB()
{
if (!m_sqlDB.InitADOServer(FilePath::GetSysDir() + _T("\\shmodel.db")))
{
AfxMessageBox(_T("打开数据库Aec3DMep.db失败!"));
EndDialog(-1);
return true;
}
return false;
}
void CModel3dLibDlg::refreshLocalFileList(HTREEITEM hItem)
{
helper_refreshLocalFileList(hItem, m_sTablePrefix);
}
void CModel3dLibDlg::refreshBimPropertyList(int idx)
{
if (idx == -1) return;
int fid = m_ctrlLocalFileList.GetItemData(idx);
m_ctrlBimPropertyList.DeleteAllItems();
int nRow(0);
SH_BIM_BLOCK blk;
getBimsFromZhSbId(m_sqlDB, fid, blk);
for (size_t i(0); i < blk.size(); ++i)
{
SH_BIM_REC &rec(blk[i]);
nRow = m_ctrlBimPropertyList.InsertItem(nRow, rec.first);
m_ctrlBimPropertyList.SetItemText(nRow, 1, rec.second);
++nRow;
}
}
void CModel3dLibDlg::LbnSelchangeFileList()
{
std::vector<int> v;
getSelectedFiles(v);
if (v.size() > 1)
return;
AcAxDocLock lock;
int idx = m_ctrlLocalFileList.GetCurSel();
refreshBimPropertyList(idx);
m_iSelectFileID = m_ctrlLocalFileList.GetItemData(idx);
CStatic *pPic = (CStatic *)GetDlgItem(IDC_PREVIEW_PIC);
if (!pPic) return;
std::shared_ptr<AcDbDatabase> pDb = getZhSbFileFromDb(m_sqlDB, m_iSelectFileID);
if (!pDb) return;
AcDbBlockTableRecordPointer pRcd(ZH_SB_BLK_NAME, pDb.get(), AcDb::kForRead);
if (pRcd.openStatus() != Acad::eOk) return;
AcDbBlockTableRecord::PreviewIcon icon;
pRcd->getPreviewIcon(icon);
if (icon.isEmpty()) return;
if (m_hBitmap)
::DeleteObject(m_hBitmap);
m_hBitmap = PreviewIcon2Bitmap(icon);
if (!m_hBitmap) return;
CImage img;
img.Attach(m_hBitmap);
CRect rect;
pPic->GetWindowRect(rect);
CPoint ptTL(rect.TopLeft());
ScreenToClient(&ptTL); // 得到绘制区域的左上角点的对话框客户端坐标
int controlWidth(rect.Width()), controlHeight(rect.Height());
int imgWidth(img.GetWidth()), imgHeight(img.GetHeight());
CDC *pDc = GetDC(); // 绘制在对话框上
CDC memDc;
memDc.CreateCompatibleDC(pDc);
CBitmap bmp;
bmp.CreateCompatibleBitmap(pDc, controlWidth, controlHeight);
CBitmap *pOldbmp = memDc.SelectObject(&bmp);
memDc.FillSolidRect(&rect, pDc->GetBkColor());
img.Draw(memDc.m_hDC, 0, 0, controlWidth, controlHeight, 0, 0, imgWidth, imgHeight);
pDc->BitBlt(ptTL.x, ptTL.y, controlWidth, controlHeight, &memDc, 0, 0, SRCCOPY);
memDc.SelectObject(pOldbmp);
ReleaseDC(pDc);
return;
}
void CModel3dLibDlg::LbnDblClkFileList()
{
if (m_bSearch)
{
helper_CurrentSelectFilePosition();
}
else
{
BnClickedInsert();
}
}
void CModel3dLibDlg::initPreviewCtrl()
{
// 不需要CAD View 预览
// 直接使用 pictrue 控件
// 无需操作,占位
}
void CModel3dLibDlg::initLocalDirTree()
{
helper_initLocalDirTree(m_sRootName, m_sTablePrefix);
}
void CModel3dLibDlg::initServerDirTree()
{
helper_initServerDirTree(_T("未找到服务器配置"));
}
void CModel3dLibDlg::BnClickedDirAdd()
{
helper_ClickedLocalDirAdd(m_sTablePrefix);
}
void CModel3dLibDlg::BnClickedDirRename()
{
helper_ClickedLocalDirRename(m_sTablePrefix);
}
void CModel3dLibDlg::BnClickedDirDel()
{
helper_ClickedLocalDirDel(m_sTablePrefix);
}
void CModel3dLibDlg::BnClickedDirOut()
{
helper_ClickedLocalDirOut(m_sTablePrefix, m_sFileSuffix);
}
void CModel3dLibDlg::BnClickedDirIn()
{
std::vector<CString> filetypes(1, CString(_T("*")) + m_sFileSuffix);
helper_ClickedLocalDirIn(m_sTablePrefix, filetypes);
}
void CModel3dLibDlg::BnClickedFileIn()
{
CString s(_T(""));
s.Format(_T("上华%s文件(*%s)|*%s||"), m_sRootName, m_sFileSuffix, m_sFileSuffix);
helper_ClickedFileIn(s);
}
void CModel3dLibDlg::BnClickedFileNew()
{
AcAxDocLock lock;
HTREEITEM hItem = m_ctrlLocalDirTree.GetSelectedItem();
if (!hItem)
{
AfxMessageBox(_T("请选中一个目录!"));
return;
}
if (!isClassItem(hItem))
{
AfxMessageBox(_T("不能在根目录或一级目录下保存文件,请选择一个二级目录!"));
return;
}
ads_name ssName;
AcDbObjectId RcdId;
AcDbObjectIdArray ids;
AcDbIdMapping idmap;
Acad::ErrorStatus es;
//SetVistualStyle(_T("Shaded"));// 提前渲染
ShowWindow(SW_HIDE);
do
{
if (acedSSGet(NULL, NULL, NULL, NULL, ssName) != RTNORM) break;
AcDbObjectId EntId;
//ads_name EntName;
Adesk::Int32 nLength = 0;
acedSSLength(ssName, &nLength);
filterX3dPartEntity(ssName, ids);
acedSSFree(ssName);
if (ids.isEmpty()) break;
AcDbExtents ext, extAll;
for (int nLen = 0; nLen < ids.length(); nLen++)
{
AcDbEntityPointer ptr(ids[nLen], AcDb::kForRead);
if (ptr.openStatus() == Acad::eOk)
{
if (ptr->getGeomExtents(ext) == Acad::eOk)
extAll.addExt(ext);
}
}
AcGePoint3d ptBlk = CoreTools::MidPt(extAll.maxPoint(), extAll.minPoint());
CString sBlockName(ZH_SB_BLK_NAME), sEquName(_T("模型1"));
if (!CoreTools::getNameByDialog(_T("输入模型名称"), _T("模型名称"), sEquName)) break;
if (sEquName.IsEmpty()) break;
if (!CoreTools::checkFileName(sEquName))
{
AfxMessageBox(_T("请检查名称,不能出现“ \\/:*?\"<>| ”。"));
break;
}
if (helper_hasSameFileName(sEquName))
{
AfxMessageBox(_T("存在相同的文件名称!"));
break;
}
if (!getAlignPoint(ptBlk)) break;
int idxClass = m_ctrlLocalDirTree.GetItemData(hItem);
SetViewportDir(AcGeVector3d(-1., -1., 1), extAll.maxPoint(), extAll.minPoint());
AcDbDatabase *_pDb = new AcDbDatabase(Adesk::kTrue, Adesk::kTrue);
std::shared_ptr<AcDbDatabase> pDb(_pDb);
// 得到当前文件中的 设置
AecDbObjXDataMap xPipeInit;
if (KTModelLayoutXData::ReadModelXData(KT_PIPEINIT, xPipeInit))
KTModelLayoutXData::WriteModelXData(KT_PIPEINIT, xPipeInit, pDb.get());
AecDbObjXDataMap xDataMap;
KTModelLayoutXData::WriteModelNameObjXData(ZH_SB_BIM_REC, xDataMap, pDb.get());
xDataMap.SetAt(_T("maxpt"), extAll.maxPoint());
xDataMap.SetAt(_T("minpt"), extAll.minPoint());
KTModelLayoutXData::WriteModelNameObjXData(ZH_SB_BLK_EXT, xDataMap, pDb.get());
AcDbBlockTableRecord *pRcd = new AcDbBlockTableRecord;
pRcd->setOrigin(ptBlk);
pRcd->setName(sBlockName);
AcDbBlockTable *pTbl(NULL);
pDb->getBlockTable(pTbl, AcDb::kForWrite);
if (!pTbl)
{
DELETE_PTR(pRcd);
break;
}
pTbl->add(RcdId, pRcd);
pTbl->close();
AcDbBlockTableRecord::PreviewIcon icon;
ScreenCopy(AcGePoint3d::kOrigin, AcGePoint3d::kOrigin, icon);
if (!icon.isEmpty())
pRcd->setPreviewIcon(icon);
pRcd->close();
Adesk::Int16 nVal = acdbCurDwg()->useri4();
acdbCurDwg()->setUseri4(KEY_NOPROTECT);
es = acdbHostApplicationServices()->workingDatabase()->wblockCloneObjects(ids
, RcdId
, idmap
, AcDb::kDrcReplace);
acdbCurDwg()->setUseri4(nVal);
CString sPath(FilePath::getSystemTemporaryFolder() + _T("\\") + sEquName + m_sFileSuffix);
if (_taccess(sPath, 0x0) == 0x0)
::DeleteFile(sPath);
#if ARX==18
es = pDb->saveAs(sPath, false, AcDb::kDHL_CURRENT);
#else
es = pDb->saveAs(sPath, false, AcDb::kDHL_1027);
#endif
if (es!=Acad::eOk) acutPrintf(_T("\n保存失败"));
helper_putEquFileToDb(m_sqlDB, sPath, idxClass);
refreshLocalFileList(hItem);
} while (false);
ShowWindow(SW_SHOW);
}
void CModel3dLibDlg::BnClickedFileCopy()
{
helper_ClickedFileCopy(m_sRootName, m_sTablePrefix);
}
void CModel3dLibDlg::BnClickedFileRename()
{
helper_ClickedFileRename(m_sTablePrefix);
}
void CModel3dLibDlg::BnClickedFileDel()
{
helper_ClickedFileDel(m_sTablePrefix);
}
void CModel3dLibDlg::BnClickedFileOut()
{
HTREEITEM hRoot = m_ctrlLocalDirTree.GetSelectedItem();
if (!hRoot)
{
AfxMessageBox(_T("请选中一个目录!"));
return;
}
std::vector<int> files;
getSelectedFiles(files);
if (files.empty())
{
AfxMessageBox(_T("请至少选择一个文件!"));
return;
}
CString sPath(_T(""));
if (!CModelLibDlgBase::helper_selectPath(_T("请选择文件要保存的目录"), sPath))
return;
CString sName(_T("")), sFile(_T(""));
int fileId(-1);
for (size_t i = 0; i < files.size(); ++i)
{
fileId = m_ctrlLocalFileList.GetItemData(files[i]);
m_ctrlLocalFileList.GetText(files[i], sName);
sName = CModelLibDlgBase::helper_checkFileName(sName);
sFile = sPath + sName;
std::shared_ptr<AcDbDatabase> ptr = getZhSbFileFromDb(m_sqlDB, fileId);
if (!ptr) return;
sFile += m_sFileSuffix;
int j(1);
while (j < 100)
{
if (_taccess(sFile, 0x0) == 0x0)
sFile.Format(_T("%s%s(%d)%s") , sPath , sName , j++, m_sFileSuffix);
else
break;
}
#if ARX==18
ptr->saveAs(sFile, false, AcDb::kDHL_CURRENT);
#else
ptr->saveAs(sFile, false, AcDb::kDHL_1027);
#endif
}
AfxMessageBox(_T("文件导出完毕。"));
}
void CModel3dLibDlg::BnClickedBimAdd()
{
int idx = m_ctrlLocalFileList.GetCurSel();
if (idx == -1)
{
AfxMessageBox(_T("请选中一个文件!"));
return;
}
int equId = m_ctrlLocalFileList.GetItemData(idx);
CString sKey(_T("")), sValue(_T(""));
while (CoreTools::getBimDataByDlg(true, sKey, sValue))
{
LVFINDINFO info;
info.flags = LVFI_PARTIAL | LVFI_STRING;
info.psz = sKey;
int nIndex;
nIndex = m_ctrlBimPropertyList.FindItem(&info);
if (nIndex != -1)
{
AfxMessageBox(_T("已经存在相同的属性!"));
return;
}
if (addBimRecToZhSbFile(m_sqlDB, equId, std::make_pair(sKey, sValue)))
{
idx = m_ctrlBimPropertyList.InsertItem(m_ctrlBimPropertyList.GetItemCount() + 1, sKey);
m_ctrlBimPropertyList.SetItemText(idx, 1, sValue);
m_ctrlBimPropertyList.Invalidate();
}
}
}
void CModel3dLibDlg::BnClickedBimModi()
{
int idx = m_ctrlLocalFileList.GetCurSel();
if (idx == -1)
{
AfxMessageBox(_T("请选中一个文件!"));
return;
}
int equId = m_ctrlLocalFileList.GetItemData(idx);
idx = m_ctrlBimPropertyList.GetSelectionMark();
if (idx == -1)
{
AfxMessageBox(_T("请选中一行数据!"));
return;
}
int bimId = m_ctrlBimPropertyList.GetItemData(idx);
if (AfxMessageBox(_T("是否修改?此操作不可逆!"), MB_YESNO) != IDYES)
return;
CString sKey(m_ctrlBimPropertyList.GetItemText(idx, 0)), sValue(m_ctrlBimPropertyList.GetItemText(idx, 1));
if (!CoreTools::getBimDataByDlg(false, sKey, sValue)) return;
if (addBimRecToZhSbFile(m_sqlDB, equId, std::make_pair(sKey, sValue)))
{
m_ctrlBimPropertyList.SetItemText(idx, 1, sValue);
m_ctrlBimPropertyList.Invalidate();
}
}
void CModel3dLibDlg::BnClickedBimDel()
{
int fileIdx = m_ctrlLocalFileList.GetCurSel();
if (fileIdx == -1)
{
AfxMessageBox(_T("请选中一个文件!"));
return;
}
int equId = m_ctrlLocalFileList.GetItemData(fileIdx);
int bimIdx = m_ctrlBimPropertyList.GetSelectionMark();
if (bimIdx == -1)
{
AfxMessageBox(_T("请选中一行数据!"));
return;
}
int bimId = m_ctrlBimPropertyList.GetItemData(bimIdx);
if (AfxMessageBox(_T("是否删除?此操作不可逆!"), MB_YESNO) != IDYES)
return;
if (delBimRecToZhSbFile(m_sqlDB, equId, m_ctrlBimPropertyList.GetItemText(bimIdx, 0)))
{
m_ctrlBimPropertyList.DeleteItem(bimIdx);
m_ctrlBimPropertyList.Invalidate();
}
}
void CModel3dLibDlg::BnClickedOk()
{
CDialog::OnOK();
}
void CModel3dLibDlg::BnClickedInsert()
{
if (m_iSelectFileID != -1)
{
SetVistualStyle(_T("2dWireframe"));
ShowWindow(SW_HIDE);
insertZHSB(m_sqlDB, m_iSelectFileID);
ShowWindow(SW_SHOW);
}
}
void CModel3dLibDlg::TvnSelchangedLocalDirTree(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
if (!pNMTreeView->itemNew.hItem) return;
refreshLocalFileList(pNMTreeView->itemNew.hItem);
m_iSelectFileID = -1;
*pResult = 0;
}
void CModel3dLibDlg::EnChangeSearch()
{
helper_EnChangeSearch(m_sTablePrefix);
}
void CModel3dLibDlg::CheckSearch()
{
CModelLibDlgBase::CheckSearch();
helper_ShowWindow(IDC_FILE_NEW, this, !m_bSearch);
}
void CModel3dLibDlg::UpdateThumbnail()
{
AcAxDocLock lock;
HTREEITEM hRoot = m_ctrlLocalDirTree.GetSelectedItem();
if (!hRoot)
{
AfxMessageBox(_T("请选中一个目录!"));
return;
}
std::vector<int> files;
getSelectedFiles(files);
if (files.size() != 1)
{
AfxMessageBox(_T("请选择一个文件!"));
return;
}
int fileId = m_ctrlLocalFileList.GetItemData(files[0]);
std::shared_ptr<AcDbDatabase> pDb = getZhSbFileFromDb(m_sqlDB, fileId);
if (!pDb) return;
AcDbBlockTableRecordPointer pRcd(ZH_SB_BLK_NAME, pDb.get(), AcDb::kForWrite);
if (pRcd.openStatus() != Acad::eOk) return;
do
{
ShowWindow(SW_HIDE);
ads_point _pt1, _pt2;
if (acedGetPoint(NULL, _T("\n选择预览图的左上角"), _pt1) != RTNORM) break;
if (acedGetPoint(_pt1, _T("\n选择预览图的右下角"), _pt2) != RTNORM) break;
AcGePoint3d pt1(asPnt3d(_pt1)), pt2(asPnt3d(_pt2));
CoreTools::UCS2WCS(pt1, pt1);
CoreTools::UCS2WCS(pt2, pt2);
AcDbExtents ext;
ext.addPoint(pt1);
ext.addPoint(pt2);
AcGePoint3d ptMax(ext.maxPoint()), ptMin(ext.minPoint());
pt1.x = ptMin.x;
pt1.y = ptMax.y;
pt2.x = ptMax.x;
pt2.y = ptMin.y;
AcDbBlockTableRecord::PreviewIcon icon;
ScreenCopy(pt1, pt2, icon);
if (icon.isEmpty()) break;
pRcd->setPreviewIcon(icon);
pRcd->close();
#if ARX==18
pDb->saveAs(pDb->originalFileName(), true, AcDb::kDHL_CURRENT);
#else
pDb->saveAs(pDb->originalFileName(), true, AcDb::kDHL_1027);
#endif
updateDwgPackage(m_sqlDB, fileId, pDb->originalFileName());
} while (false);
ShowWindow(SW_SHOW);
OnLbnSelchangeFileList();
}
void CModel3dLibDlg::PopMenuForLocalDirTree(const CPoint &pt)
{
helper_PopMenuForLocalDirTree(pt);
}
void CModel3dLibDlg::PopMenuForServerDirTree(const CPoint &pt)
{
}
void CModel3dLibDlg::PopMenuForLocalFileList(const CPoint &pt)
{
CMenu menu;
menu.CreatePopupMenu();
std::vector<int> v;
getSelectedFiles(v);
if (m_bSearch)
{
menu.AppendMenu(MF_BYCOMMAND | MF_STRING | MF_ENABLED, ID_EQU_LIB_FILE_ANCHOR, _T("定位文件(&W)"));
}
else
{
menu.AppendMenu(MF_BYCOMMAND | MF_STRING | MF_ENABLED, ID_EQU_LIB_FILE_NEW, _T("新建模型(&N)"));
menu.AppendMenu(MF_BYCOMMAND | MF_STRING | MF_ENABLED, ID_EQU_LIB_FILE_IN, _T("导入文件(&I)"));
if (!v.empty())
{
menu.AppendMenu(MF_BYCOMMAND | MF_STRING | MF_ENABLED, ID_EQU_LIB_FILE_OUT, _T("导出文件(&O)"));
menu.AppendMenu(MF_BYCOMMAND | MF_STRING | MF_ENABLED, ID_EQU_LIB_FILE_COPY, _T("复制文件(&C)"));
if (v.size() == 1)
{
menu.AppendMenu(MF_BYCOMMAND | MF_STRING | MF_ENABLED, ID_EQU_LIB_FILE_RENAME, _T("文件改名(&M)"));
menu.AppendMenu(MF_BYCOMMAND | MF_STRING | MF_ENABLED, ID_EQU_LIB_FILE_THUMBNAIL, _T("更新预览(&P)"));
}
menu.AppendMenu(MF_BYCOMMAND | MF_STRING | MF_ENABLED, ID_EQU_LIB_FILE_DEL, _T("删除文件(&D)"));
}
}
int nID = menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD
, pt.x
, pt.y, &m_ctrlLocalFileList);
switch (nID)
{
case ID_EQU_LIB_FILE_THUMBNAIL:
return UpdateThumbnail();
case ID_EQU_LIB_FILE_NEW:
OnBnClickedFileNew();
break;
case ID_EQU_LIB_FILE_IN:
OnBnClickedFileIn();
break;
case ID_EQU_LIB_FILE_OUT:
return OnBnClickedFileOut();
case ID_EQU_LIB_FILE_DEL:
OnBnClickedFileDel();
break;
case ID_EQU_LIB_FILE_COPY:
OnBnClickedFileCopy();
break;
case ID_EQU_LIB_FILE_RENAME:
OnBnClickedFileRename();
break;
case ID_EQU_LIB_FILE_ANCHOR:
return helper_CurrentSelectFilePosition();
default:
break;
}
}
void CModel3dLibDlg::shmodelgl()
{
static CModel3dLibDlg *pDlg(NULL);
if (pDlg && pDlg->GetSafeHwnd())
{
pDlg->DestroyWindow();
DELETE_PTR(pDlg);
}
CAcModuleResourceOverride myResources;
pDlg = new CModel3dLibDlg();
pDlg->Create(CModel3dLibDlg::IDD, acedGetAcadFrame());
pDlg->CenterWindow();
pDlg->ShowWindow(SW_SHOW);
}