DwgLibDialog.cpp
资源名称:suplib.rar [点击查看]
上传用户:mica555
上传日期:2014-05-08
资源大小:68k
文件大小:15k
源码类别:
CAD
开发平台:
Visual C++
- // DwgLibDialog.cpp : implementation file
- #include "StdAfx.h"
- #include "StdArx.h"
- #include "resource.h"
- #include "DwgLibDialog.h"
- #include "BlockNameDlg.h"
- #include "actrans.h"
- #include "dbsymtb.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- //11 块出库
- Adesk::Boolean outLib(char* fileName,//文件名
- char* blockName,//块名
- AcDbObjectId& blockRecId);//返回块对象Id
- //12 块入库
- Adesk::Boolean inLib(char* fileName,//文件名
- char* blockName);//块名
- //13 修改当前图形块名
- Adesk::Boolean curNewBlockName(char* newName,char* oldName);
- //14 修改外部图形块名
- Adesk::Boolean extNewBlockName(char* fName,char* newName,char* oldName);
- //-----------------------------------------------------------------------------
- IMPLEMENT_DYNAMIC (DwgLibDialog, CAcUiDialog)
- BEGIN_MESSAGE_MAP(DwgLibDialog, CAcUiDialog)
- //{{AFX_MSG_MAP(DwgLibDialog)
- ON_MESSAGE(WM_ACAD_KEEPFOCUS, OnAcadKeepFocus) // Needed for modeless dialog.
- ON_BN_CLICKED(IDINCUR, OnIncur)
- ON_BN_CLICKED(IDINLIB, OnInlib)
- ON_LBN_SELCHANGE(IDC_LIB, OnSelchangeLib)
- ON_LBN_SELCHANGE(IDC_CUR, OnSelchangeCur)
- ON_LBN_DBLCLK(IDC_CUR, OnDblclkCur)
- ON_LBN_DBLCLK(IDC_LIB, OnDblclkLib)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- //-----------------------------------------------------------------------------
- DwgLibDialog::DwgLibDialog (CWnd* pParent /*=NULL*/, HINSTANCE hInstance /*=NULL*/) : CAcUiDialog (DwgLibDialog::IDD, pParent, hInstance) {
- //{{AFX_DATA_INIT(DwgLibDialog)
- m_strcur = _T("");
- m_strlib = _T("");
- //}}AFX_DATA_INIT
- }
- void DwgLibDialog::DoDataExchange (CDataExchange *pDX) {
- CAcUiDialog::DoDataExchange (pDX) ;
- //{{AFX_DATA_MAP(DwgLibDialog)
- DDX_Control(pDX, IDC_CUR, m_cur);
- DDX_Control(pDX, IDC_LIB, m_lib);
- DDX_LBString(pDX, IDC_CUR, m_strcur);
- DDX_LBString(pDX, IDC_LIB, m_strlib);
- //}}AFX_DATA_MAP
- }
- // Needed for modeless dialogs to keep focus.
- // Return FALSE to not keep the focus, return TRUE to keep the focus
- LONG DwgLibDialog::OnAcadKeepFocus(UINT, UINT)
- {
- return TRUE;
- }
- //-----------------------------------------------------------------------------
- BOOL DwgLibDialog::OnInitDialog()
- {
- CAcUiDialog::OnInitDialog();
- CenterWindow (acedGetAcadFrame());//窗口在中心
- // TODO: Add extra initialization here
- //1 当前图形块
- //1.1 当前数据库
- AcDbBlockTable *pCurBlockTable;
- acdbHostApplicationServices()->workingDatabase()
- ->getSymbolTable(pCurBlockTable, AcDb::kForRead);
- //2 浏览块表记录
- char* pCurName=new char[500];
- AcDbBlockTableIterator *pCurBTItr;
- if (pCurBlockTable->newIterator(pCurBTItr) == Acad::eOk) {
- while (!pCurBTItr->done()) {
- AcDbBlockTableRecord *pCurRecord;
- if (pCurBTItr->getRecord(pCurRecord, AcDb::kForRead)
- == Acad::eOk) {
- pCurRecord->getName(pCurName);
- if((!pCurRecord->isAnonymous())&&(pCurName[0]!='*')&&(pCurName[0]!='_'))//不是匿名块,插入
- {
- m_cur.InsertString(-1, pCurName);
- }
- pCurRecord->close();
- }
- pCurBTItr->step();
- }
- }
- free(pCurName);
- pCurBlockTable->close();
- //当前图形块
- //图库块
- AcDbDatabase* pLibDb=new AcDbDatabase(Adesk::kFalse);
- if(pLibDb->readDwgFile("d:/wkj/wkj_dwg_lib.dwg")!=Acad::eOk)
- {
- acedAlert("找不到库文件!");
- return FALSE;
- }
- AcDbBlockTable *pLibBlockTable;
- pLibDb->getSymbolTable(pLibBlockTable, AcDb::kForRead);
- // Iterate through the block table and display
- // the names in the list box.
- //
- char *pLibName=new char[500];
- AcDbBlockTableIterator *pLibBTItr;
- if (pLibBlockTable->newIterator(pLibBTItr) == Acad::eOk) {
- while (!pLibBTItr->done()) {
- AcDbBlockTableRecord *pLibRecord;
- if (pLibBTItr->getRecord(pLibRecord, AcDb::kForRead)
- == Acad::eOk) {
- pLibRecord->getName(pLibName);
- if((!pLibRecord->isAnonymous())&&(pLibName[0]!='*')&&(pLibName[0]!='_'))
- {
- m_lib.InsertString(-1, pLibName);
- }
- pLibRecord->close();
- }
- pLibBTItr->step();
- }
- }
- pLibBlockTable->close();
- free(pLibName);
- delete pLibDb;
- pLibDb=NULL;
- //图库块
- m_cur.SetCurSel(-1);
- m_lib.SetCurSel(-1);
- GetDlgItem(IDINLIB)->EnableWindow(0);
- GetDlgItem(IDINCUR)->EnableWindow(0);
- //
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void DwgLibDialog::OnIncur()
- {
- // 从图库向当前图形插入块
- char* str_insert=new char[500];
- if((m_cur.GetCurSel()==-1)&&(m_lib.GetCurSel()!=-1))
- //1 选择图库块
- {
- strcpy(str_insert,m_strlib);
- if(m_cur.FindStringExact(-1,str_insert)==LB_ERR)
- {
- //当前图形中没有该块,插入之
- AcDbObjectId objId;
- if(outLib("d:/wkj/wkj_dwg_lib.dwg",str_insert,objId)!=Adesk::kTrue)
- {
- ::AfxMessageBox("不能出库!");
- free(str_insert);
- return;
- }
- m_cur.InsertString(-1, str_insert);
- m_cur.UpdateData(true);
- }
- }
- else
- //2选择当前图形块
- {
- strcpy(str_insert,m_strcur);
- }
- BeginEditorCommand();
- acedCommand(RTSTR,"insert",
- RTSTR,str_insert,
- 0);
- free(str_insert);
- CompleteEditorCommand();
- actrTransactionManager->queueForGraphicsFlush();
- actrTransactionManager->flushGraphics();
- CAcUiDialog::OnOK();
- }
- void DwgLibDialog::OnInlib()
- {
- // TODO: Add your control notification handler code here
- char* str_tmp=new char[500];
- strcpy(str_tmp,(LPCTSTR)m_strcur);
- if(m_lib.FindStringExact(-1,str_tmp)==LB_ERR)
- {//图库中没有该块
- if(inLib("d:/wkj/wkj_dwg_lib.dwg",str_tmp )!=Adesk::kTrue)
- {
- ::AfxMessageBox("不能入库!");
- free(str_tmp);
- return;
- }
- m_lib.InsertString(-1, m_strcur);
- UpdateData(false);
- }
- else//图库中有该块
- {
- ::AfxMessageBox("图库中已有该块!");
- free(str_tmp);
- return;
- }
- free(str_tmp);
- }
- void DwgLibDialog::OnSelchangeLib()
- {
- // TODO: Add your control notification handler code here
- m_cur.SetCurSel(-1);
- GetDlgItem(IDINLIB)->EnableWindow(0);
- GetDlgItem(IDINCUR)->EnableWindow(1);
- /*预览图
- AcDbDatabase* pLibDb=new AcDbDatabase(Adesk::kFalse);
- if(pLibDb->readDwgFile("d:/wkj/wkj_dwg_lib.dwg",_SH_DENYNO)!=Acad::eOk)
- {
- acedAlert("找不到库文件!");
- return ;
- }
- AcDbBlockTable *pLibBlockTable;
- pLibDb->getSymbolTable(pLibBlockTable, AcDb::kForRead);
- AcDbBlockTableRecord* pLibBlockTableRec;
- char tmp_str[200];
- int tmp_i;
- tmp_i=m_lib.GetCurSel();
- m_lib.GetText(tmp_i,tmp_str);
- pLibBlockTable->getAt(tmp_str,pLibBlockTableRec,AcDb::kForRead);
- pLibBlockTable->close();
- if(pLibBlockTableRec->hasPreviewIcon()==true)
- {
- AcArray<unsigned char,class AcArrayMemCopyReallocator<unsigned char> > preView;
- pLibBlockTableRec->getPreviewIcon(preView);
- acutPrintf("n 预览图大小:%d",preView.logicalLength());
- //m_preview.LoadBitmap(preView);
- pLibBlockTableRec->close();
- }
- 预览图*/
- UpdateData(true);
- }
- void DwgLibDialog::OnSelchangeCur()
- {
- // TODO: Add your control notification handler code here
- UpdateData(true);
- m_lib.SetCurSel(-1);
- //
- if(m_lib.FindStringExact(-1,m_strcur)==LB_ERR)
- {
- GetDlgItem(IDINLIB)->EnableWindow(1);
- }
- else
- {
- GetDlgItem(IDINLIB)->EnableWindow(0);
- }
- //
- GetDlgItem(IDINCUR)->EnableWindow(1);
- UpdateData(true);
- /*预览图
- AcDbBlockTable *pCurBlockTable;
- acdbHostApplicationServices()->workingDatabase()
- ->getSymbolTable(pCurBlockTable, AcDb::kForRead);
- AcDbBlockTableRecord* pCurBlockTableRec;
- char tmp_str[200];
- int tmp_i;
- tmp_i=m_cur.GetCurSel();
- m_cur.GetText(tmp_i,tmp_str);
- pCurBlockTable->getAt(tmp_str,pCurBlockTableRec,AcDb::kForRead);
- pCurBlockTable->close();
- if(pCurBlockTableRec->hasPreviewIcon()==true)
- {
- AcArray<unsigned char,class AcArrayMemCopyReallocator<unsigned char> > preView;
- pCurBlockTableRec->getPreviewIcon(preView);
- acutPrintf("n 预览图大小:%d",preView.logicalLength());
- //m_preview.LoadBitmap(preView);
- pCurBlockTableRec->close();
- }
- 预览图*/
- }
- //12 块入库
- Adesk::Boolean inLib(char* fileName,//文件名
- char* blockName)//块名
- {
- //12.1 在当前图形数据库寻找块
- AcDbBlockTable* pCurBlkTlb;
- acdbHostApplicationServices()->workingDatabase()
- ->getSymbolTable(pCurBlkTlb,AcDb::kForRead);
- AcDbObjectId curBlkTlbRcdId;
- if(pCurBlkTlb->getAt(blockName,curBlkTlbRcdId)!=Acad::eOk)
- {
- pCurBlkTlb->close();
- return Adesk::kFalse;
- }
- pCurBlkTlb->close();
- //12.2 将块写入临时数据库
- AcDbDatabase* pTmpDb;//2006-8-27:没有异常了,为什么必须这样呢?不明白啊!
- if(acdbHostApplicationServices()->workingDatabase()
- ->wblock(pTmpDb,curBlkTlbRcdId)!=Acad::eOk)
- {
- delete pTmpDb;
- return Adesk::kFalse;
- }
- //12.3 将块写入外部文件
- AcDbDatabase pExtDb(Adesk::kFalse);
- if(pExtDb.readDwgFile(fileName,_SH_DENYNO)!=Acad::eOk)
- {
- return Adesk::kFalse;
- }
- AcDbObjectId entBlkId;
- if(pExtDb.insert(entBlkId,blockName,pTmpDb)!=Acad::eOk)
- {
- delete pTmpDb;
- return Adesk::kFalse;
- }
- //12.4 外部文件存储
- if(pExtDb.saveAs(fileName)!=Acad::eOk)
- {
- delete pTmpDb;
- return Adesk::kFalse;
- }
- delete pTmpDb;
- return Adesk::kTrue;
- }
- //11 块出库
- Adesk::Boolean outLib(char* fileName,//文件名
- char* blockName,//块名
- AcDbObjectId& blockRecId)//返回块对象Id
- {
- //11.1 读入外部文件
- AcDbDatabase pExtDb(Adesk::kFalse);
- if(pExtDb.readDwgFile(fileName)!=Acad::eOk)//????
- {
- return Adesk::kFalse;
- }
- //11.2 打开外部块表
- AcDbBlockTable *pExtBlkTbl;
- if(pExtDb.getSymbolTable(pExtBlkTbl, AcDb::kForRead)!=Acad::eOk)
- {
- return Adesk::kFalse;
- }
- //11.3 外部文件是否有块
- AcDbObjectId objId=AcDbObjectId::kNull;
- if(pExtBlkTbl->getAt(blockName,objId)!=Acad::eOk)
- {
- pExtBlkTbl->close();
- return Adesk::kFalse;
- }
- //2005-08-11 修改!!!!
- pExtBlkTbl->close();
- AcDbObject* pBTRec;
- acdbOpenObject(pBTRec,objId,AcDb::kForRead);
- AcDbObjectId extDicId;
- if ((extDicId = pBTRec->extensionDictionary())
- != AcDbObjectId::kNull)
- {
- AcDbDictionary *pDict = NULL;
- acdbOpenObject(pDict, extDicId, AcDb::kForWrite);
- if (pDict) {
- pDict->setTreatElementsAsHard(Adesk::kTrue);
- pDict->close();
- }
- }
- pBTRec->close();
- //11.4生成临时数据库
- AcDbDatabase* ptmpDb;//2006-8-27:没有异常了,为什么必须这样呢?不明白啊!
- if(pExtDb.wblock(ptmpDb,objId)!=Acad::eOk)
- {
- delete ptmpDb;
- return Adesk::kFalse;
- }
- acDocManager->lockDocument(acDocManager->curDocument(), AcAp::kWrite);
- //写当前数据库之前一定要锁定
- //11.5当前图形数据库
- AcDbDatabase* pCurDb=acdbHostApplicationServices()->
- workingDatabase();
- //11.6 克隆
- if(pCurDb->insert(blockRecId,blockName, ptmpDb)!=Acad::eOk)
- {
- delete ptmpDb;
- acDocManager->unlockDocument(acDocManager->curDocument()); //解除锁定
- return Adesk::kFalse;
- }
- else
- {
- delete ptmpDb;//???
- acDocManager->unlockDocument(acDocManager->curDocument()); //解除锁定
- return Adesk::kTrue;
- }
- }
- void DwgLibDialog::OnDblclkCur()
- {
- UpdateData(true);
- g_Info.str=m_strcur;
- BlockNameDlg pBlockNameDlg;
- pBlockNameDlg.DoModal();
- }
- void DwgLibDialog::OnDblclkLib()
- {
- UpdateData(true);
- g_Info.str=m_strlib;
- BlockNameDlg pBlockNameDlg;
- pBlockNameDlg.DoModal();
- }
- //修改当前图形块名
- Adesk::Boolean curNewBlockName(char* newName,char* oldName)
- {
- //1.1 当前块表
- AcDbBlockTable* pCurBT;
- acdbHostApplicationServices()->workingDatabase()->
- getSymbolTable(pCurBT,AcDb::kForRead);
- //1.2
- AcDbObjectId curBlkId;
- if(pCurBT->getAt(oldName,curBlkId)!=Acad::eOk)
- {//没有该块
- pCurBT->close();
- acutPrintf("n当前图形中没有块:%s",oldName);
- return Adesk::kFalse;
- }
- AcDbObjectId resId;
- if(pCurBT->getAt(newName,resId)==Acad::eOk)
- {//没有该块
- pCurBT->close();
- acutPrintf("n当前图形中有新块名:%s",newName);
- return Adesk::kFalse;
- }
- pCurBT->close();
- //1.3 打开块表记录
- AcDbBlockTableRecord* pCurBTRec;
- if(acdbOpenObject(pCurBTRec,curBlkId,AcDb::kForWrite)!=Acad::eOk)
- {
- acutPrintf("n不能打开块表记录!");
- return Adesk::kFalse;
- }
- //1.4 改变块名
- if(pCurBTRec->setName(newName)!=Acad::eOk)
- {
- acutPrintf("n不能发变块名!");
- pCurBTRec->close();
- return Adesk::kFalse;
- }
- pCurBTRec->close();
- acutPrintf("n块名修改成功!");
- //
- return Adesk::kTrue;
- }
- //修改外部图形块名
- Adesk::Boolean extNewBlockName(char* fName,char* newName,char* oldName)
- {
- //注意:仅实体可进行长事务处理,符号表不可以!!!!!
- //外部图形数据库
- AcDbDatabase* pExtDb;
- pExtDb = new AcDbDatabase(Adesk::kFalse);
- if(pExtDb->readDwgFile(fName,_SH_DENYNO)!=Acad::eOk)
- {
- delete pExtDb;
- acutPrintf("n不能打开图形!");
- return Adesk::kFalse;
- }
- //外部图形数据库 块表记录
- AcDbBlockTable *pExtBT;
- pExtDb->getSymbolTable(pExtBT, AcDb::kForRead);
- AcDbObjectId extBTRecId;
- if(pExtBT->getAt(oldName, extBTRecId)!=Acad::eOk)
- {
- pExtBT->close();
- delete pExtDb;
- acutPrintf("n外部图形没有块:%s",oldName);
- return Adesk::kFalse;
- }
- AcDbObjectId refId;
- if(pExtBT->getAt(newName, refId)==Acad::eOk)
- {
- pExtBT->close();
- delete pExtDb;
- acutPrintf("n外部图形有新块名:%s",oldName);
- return Adesk::kFalse;
- }
- pExtBT->close();
- //
- AcDbBlockTableRecord* pExtBTRec;
- if(acdbOpenObject(pExtBTRec,extBTRecId,AcDb::kForWrite)!=Acad::eOk)
- {
- delete pExtDb;
- acutPrintf("n不能打开块!");
- }
- pExtBTRec->setName(newName);
- pExtBTRec->close();
- pExtDb->saveAs(fName);
- delete pExtDb;
- pExtDb=NULL;
- acutPrintf("n块名修改成功!");
- return Adesk::kTrue;
- }