#include "stdafx.h" #include "ShsBlockToolsDlg.h" #include "coretools.h" namespace { CString selectDWGFilePath() { CFileDialog dlg(TRUE, _T("*.dwg") , NULL , OFN_HIDEREADONLY , _T("AutoCAD dwg文件 (*.dwg)|*.dwg||") , acedGetAcadFrame()); if (dlg.DoModal() != IDOK) return _T(""); return dlg.GetPathName(); } void refreshList(AcDbDatabase *pDb, CListBox &wndList) { if (!pDb) return; wndList.ResetContent(); std::vector vec; CoreTools::enumBlockByDWG(pDb, vec); for (size_t i(0); i < vec.size(); ++i) { wndList.AddString(vec[i]); } } bool openDWGFile(LPCTSTR pszDWG, AcDbDatabase *&pDb) { if (_taccess(pszDWG, 0x0) != 0x0) return false; pDb = new AcDbDatabase(false); if (pDb->readDwgFile(pszDWG, AcDbDatabase::kForReadAndAllShare) == Acad::eOk) return true; else DELETE_PTR(pDb); return false; } bool renameBlockRec(AcDbDatabase *pDb, LPCTSTR pszOldBlk, LPCTSTR pszNewBlk) { if (!pDb || !pszOldBlk || !pszNewBlk) return false; bool bRel(false); AcDbBlockTable *pTbl(NULL); pDb->getBlockTable(pTbl, AcDb::kForRead); if (pTbl) { AcDbBlockTableRecord *pRcd(NULL); pTbl->getAt(pszOldBlk, pRcd, AcDb::kForWrite); if (pRcd) { bRel = pRcd->setName(pszNewBlk) == Acad::eOk; pRcd->close(); } pTbl->close(); } return bRel; } bool deleteBlockRec(AcDbDatabase *pDb, LPCTSTR pszBlk) { if (!pDb || !pszBlk) return false; bool bRel(false); AcDbBlockTable *pTbl(NULL); pDb->getBlockTable(pTbl, AcDb::kForRead); if (pTbl) { AcDbBlockTableRecord *pRcd(NULL); pTbl->getAt(pszBlk, pRcd, AcDb::kForWrite); if (pRcd) { bRel = pRcd->erase() == Acad::eOk; pRcd->close(); } pTbl->close(); } return bRel; } CString getSelectString(CListBox &list) { int idx = list.GetCurSel(); if (idx == -1) { AfxMessageBox(_T("请选择一行数据!")); return _T(""); } CString str(_T("")); list.GetText(idx, str); return str; } AcDbObjectId getBlkID(AcDbDatabase *pDb, LPCTSTR pszBlk) { AcDbObjectId id(AcDbObjectId::kNull); if (!pDb || !pszBlk) return id; AcDbBlockTable *pTbl(NULL); pDb->getBlockTable(pTbl, AcDb::kForRead); if (pTbl) { pTbl->getAt(pszBlk, id); pTbl->close(); } return id; } void refreshPreviewCtrl(CGsPreviewCtrl &preview, AcDbObjectId &id) { AcDbBlockReference *pRef = new AcDbBlockReference(); pRef->setBlockTableRecord(id); pRef->setPosition(AcGePoint3d::kOrigin); pRef->setRotation(0.0); AcDbExtents ext; pRef->getGeomExtents(ext); pRef->setScaleFactors(AcGeScale3d(.8)); preview.clearAll(); preview.init(true); preview.SetOrbitFlag(OBF_ZOOM); preview.InitFromCurrentView2D(ext, true); preview.Add(pRef); preview.view()->invalidate(); preview.Invalidate(); preview.RedrawWindow(); } AcDbObjectId getSelectID(AcDbDatabase *pDb, CListBox &wndList) { CString sName(getSelectString(wndList)); if (sName.IsEmpty()) return AcDbObjectId::kNull; return getBlkID(pDb, sName); } bool hasBlock(AcDbDatabase *pDb, LPCTSTR pszBlk) { if (!pDb || !pszBlk) return false; bool bHas(false); AcDbObjectId id(AcDbObjectId::kNull); AcDbBlockTable *pTbl(NULL); pDb->getBlockTable(pTbl, AcDb::kForRead); if (pTbl) { bHas = pTbl->has(pszBlk); pTbl->close(); } return bHas; } bool copyBlock(AcDbDatabase *pSrc, AcDbDatabase *pDst, LPCTSTR pszBlk) { if (!pSrc || !pDst || !pszBlk) return false; if (hasBlock(pDst, pszBlk)) { AfxMessageBox(_T("块已经存在,请改名后重试!")); return false; } AcDbObjectId id(getBlkID(pSrc, pszBlk)), newID; if (!id.isValid()) return false; AcDbDatabase *pTempDb(NULL); if (pSrc->wblock(pTempDb, id) != Acad::eOk) return false; bool bRel(false); if (pTempDb) { bRel = pDst->insert(newID, pszBlk, pTempDb) == Acad::eOk; DELETE_PTR(pTempDb); } return bRel; } void saveAsDWG(AcDbDatabase *pDb) { if (!pDb) return; const TCHAR* pszFile = NULL; pDb->getFilename(pszFile); Acad::ErrorStatus es = pDb->saveAs(pszFile,false,AcDb::kDHL_1021); if (es != Acad::eOk) acutPrintf(_T("\nsaveAs res = %d"),es); } void closeDbFile(AcDbDatabase *&pDb) { saveAsDWG(pDb); DELETE_PTR(pDb); } } IMPLEMENT_DYNAMIC(CShsBlockToolsDlg, CDialog) CShsBlockToolsDlg::CShsBlockToolsDlg(CWnd* pParent /*=nullptr*/) : CDialog(IDD_BLOCK_TOOLS_DLG, pParent) , m_sPathLeft(_T("")) , m_sPathRight(_T("")) , m_pDbLeft(NULL) , m_pDbRight(NULL) { } CShsBlockToolsDlg::~CShsBlockToolsDlg() { DELETE_PTR(m_pDbLeft); DELETE_PTR(m_pDbRight); } void CShsBlockToolsDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_LIST_LEFT, m_wndListLeft); DDX_Control(pDX, IDC_LIST_RIGHT, m_wndListRight); DDX_Text(pDX, IDC_EDT_PATH_LEFT, m_sPathLeft); DDX_Text(pDX, IDC_EDT_PATH_RIGHT, m_sPathRight); } BOOL CShsBlockToolsDlg::OnInitDialog() { CDialog::OnInitDialog(); m_wndPreViewLeft.SubclassDlgItem(IDC_PREVIEW_LEFT, this); m_wndPreViewRight.SubclassDlgItem(IDC_PREVIEW_RIGHT, this); return TRUE; } BEGIN_MESSAGE_MAP(CShsBlockToolsDlg, CDialog) ON_BN_CLICKED(IDOK, &CShsBlockToolsDlg::OnBnClickedOk) ON_BN_CLICKED(IDCANCEL, &CShsBlockToolsDlg::OnBnClickedCancel) ON_BN_CLICKED(IDC_BTN_LOAD_LFET, &CShsBlockToolsDlg::OnBnClickedBtnLoadLfet) ON_BN_CLICKED(IDC_BTN_LOAD_RIGHT, &CShsBlockToolsDlg::OnBnClickedBtnLoadRight) ON_BN_CLICKED(IDC_BTN_RENAME_LEFT, &CShsBlockToolsDlg::OnBnClickedBtnRenameLeft) ON_BN_CLICKED(IDC_BTN_RENAME_RIGHT, &CShsBlockToolsDlg::OnBnClickedBtnRenameRight) ON_BN_CLICKED(IDC_BTN_DEL_LEFT, &CShsBlockToolsDlg::OnBnClickedBtnDelLeft) ON_BN_CLICKED(IDC_BTN_DEL_RIGHT, &CShsBlockToolsDlg::OnBnClickedBtnDelRight) ON_BN_CLICKED(IDC_BTN_LEFT2RIGHT, &CShsBlockToolsDlg::OnBnClickedBtnLeft2right) ON_BN_CLICKED(IDC_BTN_RIGHT2LEFT, &CShsBlockToolsDlg::OnBnClickedBtnRight2left) ON_LBN_SELCHANGE(IDC_LIST_LEFT, &CShsBlockToolsDlg::OnLbnSelchangeListLeft) ON_LBN_SELCHANGE(IDC_LIST_RIGHT, &CShsBlockToolsDlg::OnLbnSelchangeListRight) ON_LBN_DBLCLK(IDC_LIST_LEFT, &CShsBlockToolsDlg::OnLbnDblclkListLeft) ON_LBN_DBLCLK(IDC_LIST_RIGHT, &CShsBlockToolsDlg::OnLbnDblclkListRight) END_MESSAGE_MAP() void CShsBlockToolsDlg::OnBnClickedBtnLoadLfet() { closeDbFile(m_pDbLeft); if (m_pDbLeft) return; CString sPath = selectDWGFilePath(); if (_taccess(sPath, 0x0) != 0x0) { AfxMessageBox(_T("文件不存在!")); return; } if (!openDWGFile(sPath, m_pDbLeft)) return; refreshList(m_pDbLeft, m_wndListLeft); m_sPathLeft = sPath; UpdateData(FALSE); } void CShsBlockToolsDlg::OnBnClickedBtnLoadRight() { closeDbFile(m_pDbRight); if (m_pDbRight) return; CString sPath = selectDWGFilePath(); if (_taccess(sPath, 0x0) != 0x0) { AfxMessageBox(_T("文件不存在!")); return; } if (!openDWGFile(sPath, m_pDbRight)) return; refreshList(m_pDbRight, m_wndListRight); m_sPathRight = sPath; UpdateData(FALSE); } void CShsBlockToolsDlg::OnBnClickedBtnRenameLeft() { CString sName(getSelectString(m_wndListLeft)), sOldName; if (sName.IsEmpty()) return; sOldName = sName; if (CoreTools::getNameByDialog(_T("新名称"), _T("块名称"), sName) && !sName.IsEmpty() && sName != sOldName) { if (hasBlock(m_pDbLeft, sName)) { AfxMessageBox(_T("块名已经存在!")); return; } if (renameBlockRec(m_pDbLeft, sOldName, sName)) refreshList(m_pDbLeft, m_wndListLeft); } } void CShsBlockToolsDlg::OnBnClickedBtnRenameRight() { CString sName(getSelectString(m_wndListRight)), sOldName; if (sName.IsEmpty()) return; sOldName = sName; if (CoreTools::getNameByDialog(_T("新名称"), _T("块名称"), sName) && !sName.IsEmpty() && sName != sOldName) { if (hasBlock(m_pDbRight, sName)) { AfxMessageBox(_T("块名已经存在!")); return; } if (renameBlockRec(m_pDbRight, sOldName, sName)) refreshList(m_pDbRight, m_wndListRight); } } void CShsBlockToolsDlg::OnBnClickedBtnDelLeft() { if (AfxMessageBox(_T("此操作不可恢复,是否删除?"), MB_YESNO) != IDYES) return; CString sName(getSelectString(m_wndListLeft)); if (sName.IsEmpty()) return; if (deleteBlockRec(m_pDbLeft, sName)) refreshList(m_pDbLeft, m_wndListLeft); } void CShsBlockToolsDlg::OnBnClickedBtnDelRight() { if (AfxMessageBox(_T("此操作不可恢复,是否删除?"), MB_YESNO) != IDYES) return; CString sName(getSelectString(m_wndListRight)); if (sName.IsEmpty()) return; if (deleteBlockRec(m_pDbRight, sName)) refreshList(m_pDbRight, m_wndListRight); } void CShsBlockToolsDlg::OnBnClickedBtnLeft2right() { CString sName(getSelectString(m_wndListLeft)); if (sName.IsEmpty()) return; if (copyBlock(m_pDbLeft, m_pDbRight, sName)) refreshList(m_pDbRight, m_wndListRight); } void CShsBlockToolsDlg::OnBnClickedBtnRight2left() { CString sName(getSelectString(m_wndListRight)); if (sName.IsEmpty()) return; if (copyBlock(m_pDbRight, m_pDbLeft, sName)) refreshList(m_pDbLeft, m_wndListLeft); } void CShsBlockToolsDlg::OnLbnSelchangeListLeft() { AcDbObjectId id = getSelectID(m_pDbLeft, m_wndListLeft); if (!id.isValid()) return; refreshPreviewCtrl(m_wndPreViewLeft, id); } void CShsBlockToolsDlg::OnLbnSelchangeListRight() { AcDbObjectId id = getSelectID(m_pDbRight, m_wndListRight); if (!id.isValid()) return; refreshPreviewCtrl(m_wndPreViewRight, id); } void CShsBlockToolsDlg::OnLbnDblclkListLeft() { OnBnClickedBtnRenameLeft(); } void CShsBlockToolsDlg::OnLbnDblclkListRight() { OnBnClickedBtnRenameRight(); } void CShsBlockToolsDlg::OnBnClickedOk() { CDialog::OnOK(); closeDbFile(m_pDbLeft); closeDbFile(m_pDbRight); } void CShsBlockToolsDlg::OnBnClickedCancel() { DELETE_PTR(m_pDbLeft); DELETE_PTR(m_pDbRight); CDialog::OnCancel(); } void CShsBlockToolsDlg::start() { CAcModuleResourceOverride myResources; CShsBlockToolsDlg dlg; dlg.DoModal(); }