DwgLibDialog.cpp
上传用户:mica555
上传日期:2014-05-08
资源大小:68k
文件大小:15k
源码类别:

CAD

开发平台:

Visual C++

  1. // DwgLibDialog.cpp : implementation file
  2. #include "StdAfx.h"
  3. #include "StdArx.h"
  4. #include "resource.h"
  5. #include "DwgLibDialog.h"
  6. #include "BlockNameDlg.h"
  7. #include "actrans.h"
  8. #include "dbsymtb.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. //11 块出库
  15. Adesk::Boolean outLib(char* fileName,//文件名
  16. char* blockName,//块名
  17. AcDbObjectId& blockRecId);//返回块对象Id
  18. //12 块入库
  19. Adesk::Boolean inLib(char* fileName,//文件名
  20. char* blockName);//块名
  21. //13 修改当前图形块名
  22. Adesk::Boolean curNewBlockName(char* newName,char* oldName);
  23. //14 修改外部图形块名
  24. Adesk::Boolean extNewBlockName(char* fName,char* newName,char* oldName);
  25. //-----------------------------------------------------------------------------
  26. IMPLEMENT_DYNAMIC (DwgLibDialog, CAcUiDialog)
  27. BEGIN_MESSAGE_MAP(DwgLibDialog, CAcUiDialog)
  28. //{{AFX_MSG_MAP(DwgLibDialog)
  29. ON_MESSAGE(WM_ACAD_KEEPFOCUS, OnAcadKeepFocus)    // Needed for modeless dialog.
  30. ON_BN_CLICKED(IDINCUR, OnIncur)
  31. ON_BN_CLICKED(IDINLIB, OnInlib)
  32. ON_LBN_SELCHANGE(IDC_LIB, OnSelchangeLib)
  33. ON_LBN_SELCHANGE(IDC_CUR, OnSelchangeCur)
  34. ON_LBN_DBLCLK(IDC_CUR, OnDblclkCur)
  35. ON_LBN_DBLCLK(IDC_LIB, OnDblclkLib)
  36. //}}AFX_MSG_MAP
  37. END_MESSAGE_MAP()
  38. //-----------------------------------------------------------------------------
  39. DwgLibDialog::DwgLibDialog (CWnd* pParent /*=NULL*/, HINSTANCE hInstance /*=NULL*/) : CAcUiDialog (DwgLibDialog::IDD, pParent, hInstance) {
  40. //{{AFX_DATA_INIT(DwgLibDialog)
  41. m_strcur = _T("");
  42. m_strlib = _T("");
  43. //}}AFX_DATA_INIT
  44. }
  45. void DwgLibDialog::DoDataExchange (CDataExchange *pDX) {
  46. CAcUiDialog::DoDataExchange (pDX) ;
  47. //{{AFX_DATA_MAP(DwgLibDialog)
  48. DDX_Control(pDX, IDC_CUR, m_cur);
  49. DDX_Control(pDX, IDC_LIB, m_lib);
  50. DDX_LBString(pDX, IDC_CUR, m_strcur);
  51. DDX_LBString(pDX, IDC_LIB, m_strlib);
  52. //}}AFX_DATA_MAP
  53. }
  54. // Needed for modeless dialogs to keep focus.
  55. // Return FALSE to not keep the focus, return TRUE to keep the focus
  56. LONG DwgLibDialog::OnAcadKeepFocus(UINT, UINT)
  57. {
  58.   return TRUE;
  59. }
  60. //-----------------------------------------------------------------------------
  61. BOOL DwgLibDialog::OnInitDialog() 
  62. {
  63. CAcUiDialog::OnInitDialog();
  64. CenterWindow  (acedGetAcadFrame());//窗口在中心
  65. // TODO: Add extra initialization here
  66.      
  67.     //1 当前图形块
  68.     //1.1 当前数据库
  69.     AcDbBlockTable *pCurBlockTable;
  70.     acdbHostApplicationServices()->workingDatabase()
  71.         ->getSymbolTable(pCurBlockTable, AcDb::kForRead);
  72.     //2 浏览块表记录
  73.     char* pCurName=new char[500];
  74.     AcDbBlockTableIterator *pCurBTItr;
  75.     if (pCurBlockTable->newIterator(pCurBTItr) == Acad::eOk) {
  76.         while (!pCurBTItr->done()) {
  77.             AcDbBlockTableRecord *pCurRecord;
  78.             if (pCurBTItr->getRecord(pCurRecord, AcDb::kForRead)
  79.                 == Acad::eOk) {
  80.                 pCurRecord->getName(pCurName);
  81.                 if((!pCurRecord->isAnonymous())&&(pCurName[0]!='*')&&(pCurName[0]!='_'))//不是匿名块,插入
  82.                 {
  83.                 m_cur.InsertString(-1, pCurName);
  84.                 }
  85.                 pCurRecord->close();
  86.             }
  87.             pCurBTItr->step();
  88.         }
  89.     }
  90.     free(pCurName);
  91.     pCurBlockTable->close();
  92.     //当前图形块
  93.     //图库块
  94.     AcDbDatabase* pLibDb=new AcDbDatabase(Adesk::kFalse);
  95.     if(pLibDb->readDwgFile("d:/wkj/wkj_dwg_lib.dwg")!=Acad::eOk)
  96.     {
  97.       acedAlert("找不到库文件!");
  98.       return FALSE;
  99.     }
  100.     AcDbBlockTable *pLibBlockTable;
  101.     pLibDb->getSymbolTable(pLibBlockTable, AcDb::kForRead);
  102.     // Iterate through the block table and display 
  103.     // the names in the list box.
  104.     //
  105.     char *pLibName=new char[500];
  106.     AcDbBlockTableIterator *pLibBTItr;
  107.     if (pLibBlockTable->newIterator(pLibBTItr) == Acad::eOk) {
  108.         while (!pLibBTItr->done()) {
  109.             AcDbBlockTableRecord *pLibRecord;
  110.             if (pLibBTItr->getRecord(pLibRecord, AcDb::kForRead)
  111.                 == Acad::eOk) {
  112.                 pLibRecord->getName(pLibName);
  113.                 if((!pLibRecord->isAnonymous())&&(pLibName[0]!='*')&&(pLibName[0]!='_'))
  114.                 {
  115.                 m_lib.InsertString(-1, pLibName);
  116.                 }
  117.                 pLibRecord->close();
  118.             }
  119.             pLibBTItr->step();
  120.         }
  121.     }
  122.     pLibBlockTable->close();
  123.     free(pLibName);
  124.     delete pLibDb;
  125.     pLibDb=NULL;
  126. //图库块
  127.     m_cur.SetCurSel(-1);
  128.     m_lib.SetCurSel(-1);
  129.     GetDlgItem(IDINLIB)->EnableWindow(0);
  130.     GetDlgItem(IDINCUR)->EnableWindow(0);
  131.     //
  132. return TRUE;  // return TRUE unless you set the focus to a control
  133.               // EXCEPTION: OCX Property Pages should return FALSE
  134. }
  135. void DwgLibDialog::OnIncur() 
  136. {
  137. // 从图库向当前图形插入块
  138.     char* str_insert=new char[500];
  139.     if((m_cur.GetCurSel()==-1)&&(m_lib.GetCurSel()!=-1))
  140.     //1 选择图库块
  141.     { 
  142.        strcpy(str_insert,m_strlib);
  143.        if(m_cur.FindStringExact(-1,str_insert)==LB_ERR)
  144.        {
  145.          //当前图形中没有该块,插入之
  146.          AcDbObjectId objId;
  147.          if(outLib("d:/wkj/wkj_dwg_lib.dwg",str_insert,objId)!=Adesk::kTrue)
  148.          {
  149.           ::AfxMessageBox("不能出库!");
  150.           free(str_insert);
  151.           return;
  152.          }
  153.          m_cur.InsertString(-1, str_insert);
  154.          m_cur.UpdateData(true);
  155.        }
  156.      }
  157.      else
  158.      //2选择当前图形块
  159.      {
  160.        strcpy(str_insert,m_strcur);
  161.      }
  162.     BeginEditorCommand();
  163.     acedCommand(RTSTR,"insert",
  164.                 RTSTR,str_insert,
  165.                 0);  
  166.     free(str_insert);
  167.     CompleteEditorCommand();
  168. actrTransactionManager->queueForGraphicsFlush();
  169. actrTransactionManager->flushGraphics();
  170.     CAcUiDialog::OnOK();
  171. }
  172. void DwgLibDialog::OnInlib() 
  173. {
  174. // TODO: Add your control notification handler code here
  175.     char* str_tmp=new char[500];
  176.     strcpy(str_tmp,(LPCTSTR)m_strcur);
  177.     if(m_lib.FindStringExact(-1,str_tmp)==LB_ERR)
  178.     {//图库中没有该块
  179.       if(inLib("d:/wkj/wkj_dwg_lib.dwg",str_tmp )!=Adesk::kTrue)
  180.       {
  181.        ::AfxMessageBox("不能入库!");
  182.        free(str_tmp);
  183.        return;
  184.       }
  185.   m_lib.InsertString(-1, m_strcur);
  186.       UpdateData(false);
  187.     }
  188.     else//图库中有该块
  189.     {
  190.       ::AfxMessageBox("图库中已有该块!");
  191.       free(str_tmp);
  192.       return;
  193.     }
  194.     free(str_tmp);
  195. }
  196. void DwgLibDialog::OnSelchangeLib() 
  197. {
  198. // TODO: Add your control notification handler code here
  199.     m_cur.SetCurSel(-1);
  200.     GetDlgItem(IDINLIB)->EnableWindow(0);
  201.     GetDlgItem(IDINCUR)->EnableWindow(1);
  202.    
  203.     /*预览图
  204.     AcDbDatabase* pLibDb=new AcDbDatabase(Adesk::kFalse);
  205.     if(pLibDb->readDwgFile("d:/wkj/wkj_dwg_lib.dwg",_SH_DENYNO)!=Acad::eOk)
  206.     {
  207.       acedAlert("找不到库文件!");
  208.       return ;
  209.     }
  210.     AcDbBlockTable *pLibBlockTable;
  211.     pLibDb->getSymbolTable(pLibBlockTable, AcDb::kForRead);
  212.     AcDbBlockTableRecord* pLibBlockTableRec;
  213.     char tmp_str[200];
  214.     int tmp_i;
  215.     tmp_i=m_lib.GetCurSel();
  216.     m_lib.GetText(tmp_i,tmp_str);
  217.     pLibBlockTable->getAt(tmp_str,pLibBlockTableRec,AcDb::kForRead);
  218.     pLibBlockTable->close();
  219.     if(pLibBlockTableRec->hasPreviewIcon()==true)
  220.     {
  221.     AcArray<unsigned char,class AcArrayMemCopyReallocator<unsigned char> > preView;
  222.     pLibBlockTableRec->getPreviewIcon(preView);
  223.     acutPrintf("n 预览图大小:%d",preView.logicalLength());
  224.     //m_preview.LoadBitmap(preView);
  225.     pLibBlockTableRec->close();
  226.     }
  227.     预览图*/
  228.     
  229. UpdateData(true);
  230. }
  231. void DwgLibDialog::OnSelchangeCur() 
  232. {
  233. // TODO: Add your control notification handler code here
  234.     UpdateData(true);
  235.     m_lib.SetCurSel(-1);
  236.     //
  237.     
  238.     if(m_lib.FindStringExact(-1,m_strcur)==LB_ERR)
  239.         {
  240.         GetDlgItem(IDINLIB)->EnableWindow(1);
  241.         }
  242.     else
  243.         {
  244.         GetDlgItem(IDINLIB)->EnableWindow(0);
  245.         }
  246.     
  247.     //
  248.     GetDlgItem(IDINCUR)->EnableWindow(1);
  249. UpdateData(true);
  250.     /*预览图
  251.     
  252.     AcDbBlockTable *pCurBlockTable;
  253.     acdbHostApplicationServices()->workingDatabase()
  254.           ->getSymbolTable(pCurBlockTable, AcDb::kForRead);
  255.     AcDbBlockTableRecord* pCurBlockTableRec;
  256.     char tmp_str[200];
  257.     int tmp_i;
  258.     tmp_i=m_cur.GetCurSel();
  259.     m_cur.GetText(tmp_i,tmp_str);
  260.     pCurBlockTable->getAt(tmp_str,pCurBlockTableRec,AcDb::kForRead);
  261.     pCurBlockTable->close();
  262.     if(pCurBlockTableRec->hasPreviewIcon()==true)
  263.     {
  264.     AcArray<unsigned char,class AcArrayMemCopyReallocator<unsigned char> > preView;
  265.     pCurBlockTableRec->getPreviewIcon(preView);
  266.     acutPrintf("n 预览图大小:%d",preView.logicalLength());
  267.     //m_preview.LoadBitmap(preView);
  268.     pCurBlockTableRec->close();
  269.     }
  270.     预览图*/
  271. }
  272. //12 块入库
  273. Adesk::Boolean inLib(char* fileName,//文件名
  274. char* blockName)//块名
  275. {
  276.    
  277.    //12.1 在当前图形数据库寻找块
  278.    AcDbBlockTable* pCurBlkTlb;
  279.    acdbHostApplicationServices()->workingDatabase()
  280.        ->getSymbolTable(pCurBlkTlb,AcDb::kForRead);
  281.    AcDbObjectId curBlkTlbRcdId;
  282.    if(pCurBlkTlb->getAt(blockName,curBlkTlbRcdId)!=Acad::eOk)
  283.    {
  284.      pCurBlkTlb->close();
  285.      return Adesk::kFalse;
  286.    }
  287.    pCurBlkTlb->close();
  288.    //12.2 将块写入临时数据库
  289.    AcDbDatabase* pTmpDb;//2006-8-27:没有异常了,为什么必须这样呢?不明白啊!
  290.    if(acdbHostApplicationServices()->workingDatabase()
  291.       ->wblock(pTmpDb,curBlkTlbRcdId)!=Acad::eOk)
  292.    {
  293.      delete pTmpDb;
  294.      return Adesk::kFalse;
  295.    }
  296.    //12.3 将块写入外部文件
  297.    AcDbDatabase pExtDb(Adesk::kFalse);
  298.    if(pExtDb.readDwgFile(fileName,_SH_DENYNO)!=Acad::eOk)
  299.    {
  300.  
  301.      return Adesk::kFalse;
  302.    }
  303.    AcDbObjectId entBlkId;
  304.    if(pExtDb.insert(entBlkId,blockName,pTmpDb)!=Acad::eOk)
  305.    {
  306.      delete pTmpDb;
  307.      return Adesk::kFalse;
  308.    }
  309.    //12.4 外部文件存储
  310.    if(pExtDb.saveAs(fileName)!=Acad::eOk)
  311.    {
  312.      delete pTmpDb;
  313.      return Adesk::kFalse;
  314.    }
  315.    delete pTmpDb;
  316.    return Adesk::kTrue;
  317. }
  318. //11 块出库
  319. Adesk::Boolean outLib(char* fileName,//文件名
  320. char* blockName,//块名
  321. AcDbObjectId& blockRecId)//返回块对象Id
  322. {
  323.    //11.1 读入外部文件
  324.    AcDbDatabase pExtDb(Adesk::kFalse);
  325.    if(pExtDb.readDwgFile(fileName)!=Acad::eOk)//????
  326.    {
  327.      return Adesk::kFalse;
  328.    }
  329.    //11.2 打开外部块表
  330.    AcDbBlockTable *pExtBlkTbl;
  331.    if(pExtDb.getSymbolTable(pExtBlkTbl, AcDb::kForRead)!=Acad::eOk)
  332.    {
  333.       return Adesk::kFalse;
  334.    }
  335.    //11.3 外部文件是否有块
  336.    AcDbObjectId objId=AcDbObjectId::kNull;
  337.    if(pExtBlkTbl->getAt(blockName,objId)!=Acad::eOk)
  338.    {
  339.       pExtBlkTbl->close();
  340.   return Adesk::kFalse;
  341.    }
  342.    //2005-08-11  修改!!!!
  343.    pExtBlkTbl->close();
  344.    AcDbObject* pBTRec;
  345.    acdbOpenObject(pBTRec,objId,AcDb::kForRead);
  346.    AcDbObjectId extDicId;
  347.    if ((extDicId = pBTRec->extensionDictionary())
  348.       != AcDbObjectId::kNull)
  349.     {
  350.     AcDbDictionary *pDict = NULL;
  351.     acdbOpenObject(pDict, extDicId, AcDb::kForWrite);
  352.     if (pDict) {
  353.         pDict->setTreatElementsAsHard(Adesk::kTrue);
  354.         pDict->close();
  355.         }
  356.     }
  357.     pBTRec->close();
  358.     //11.4生成临时数据库
  359.     AcDbDatabase* ptmpDb;//2006-8-27:没有异常了,为什么必须这样呢?不明白啊!
  360.     if(pExtDb.wblock(ptmpDb,objId)!=Acad::eOk)
  361. {
  362.       delete ptmpDb;
  363.       return Adesk::kFalse;
  364. }
  365.     acDocManager->lockDocument(acDocManager->curDocument(), AcAp::kWrite); 
  366.     //写当前数据库之前一定要锁定
  367.     //11.5当前图形数据库
  368.     AcDbDatabase* pCurDb=acdbHostApplicationServices()->
  369.                           workingDatabase();
  370.     //11.6 克隆
  371.     if(pCurDb->insert(blockRecId,blockName, ptmpDb)!=Acad::eOk)
  372. {
  373.       delete ptmpDb;
  374.       acDocManager->unlockDocument(acDocManager->curDocument()); //解除锁定
  375.       return Adesk::kFalse;
  376. }
  377.     else
  378. {
  379.        delete ptmpDb;//???
  380.        acDocManager->unlockDocument(acDocManager->curDocument()); //解除锁定
  381.        return Adesk::kTrue;
  382. }
  383. }
  384. void DwgLibDialog::OnDblclkCur() 
  385. {
  386.     UpdateData(true);
  387. g_Info.str=m_strcur;
  388. BlockNameDlg pBlockNameDlg;
  389.     pBlockNameDlg.DoModal();
  390. }
  391. void DwgLibDialog::OnDblclkLib() 
  392. {
  393.     UpdateData(true);
  394.     g_Info.str=m_strlib;
  395. BlockNameDlg pBlockNameDlg;
  396.     pBlockNameDlg.DoModal();
  397. }
  398. //修改当前图形块名
  399. Adesk::Boolean curNewBlockName(char* newName,char* oldName)
  400.     {
  401.     //1.1 当前块表
  402.     AcDbBlockTable* pCurBT;
  403.     acdbHostApplicationServices()->workingDatabase()->
  404.         getSymbolTable(pCurBT,AcDb::kForRead);
  405.     //1.2
  406.     AcDbObjectId curBlkId;
  407.     if(pCurBT->getAt(oldName,curBlkId)!=Acad::eOk)
  408.         {//没有该块
  409.         pCurBT->close();
  410.         acutPrintf("n当前图形中没有块:%s",oldName);
  411.         return Adesk::kFalse;
  412.         }
  413.     AcDbObjectId resId;
  414.     if(pCurBT->getAt(newName,resId)==Acad::eOk)
  415.         {//没有该块
  416.         pCurBT->close();
  417.         acutPrintf("n当前图形中有新块名:%s",newName);
  418.         return Adesk::kFalse;
  419.         }
  420.     pCurBT->close();
  421.     //1.3 打开块表记录
  422.     AcDbBlockTableRecord* pCurBTRec;
  423.     if(acdbOpenObject(pCurBTRec,curBlkId,AcDb::kForWrite)!=Acad::eOk)
  424.         {
  425.         acutPrintf("n不能打开块表记录!");
  426.         return Adesk::kFalse;
  427.         }
  428.     //1.4 改变块名
  429.     if(pCurBTRec->setName(newName)!=Acad::eOk)
  430.         {
  431.         acutPrintf("n不能发变块名!");
  432.         pCurBTRec->close();
  433.         return Adesk::kFalse;
  434.         }
  435.     pCurBTRec->close();
  436.     acutPrintf("n块名修改成功!");
  437.     //
  438.     return Adesk::kTrue;
  439.     }
  440. //修改外部图形块名
  441. Adesk::Boolean extNewBlockName(char* fName,char* newName,char* oldName)
  442.     {
  443.     
  444.     //注意:仅实体可进行长事务处理,符号表不可以!!!!!
  445.     //外部图形数据库
  446.     AcDbDatabase* pExtDb;
  447.     pExtDb = new AcDbDatabase(Adesk::kFalse);
  448.     if(pExtDb->readDwgFile(fName,_SH_DENYNO)!=Acad::eOk)
  449.         {
  450.         delete pExtDb;
  451.         acutPrintf("n不能打开图形!");
  452.         return Adesk::kFalse;
  453.         }
  454.     //外部图形数据库 块表记录
  455.     AcDbBlockTable *pExtBT;
  456.     pExtDb->getSymbolTable(pExtBT, AcDb::kForRead);
  457.     AcDbObjectId extBTRecId;
  458.     if(pExtBT->getAt(oldName, extBTRecId)!=Acad::eOk)
  459.         {
  460.         pExtBT->close();
  461.         delete pExtDb;
  462.         acutPrintf("n外部图形没有块:%s",oldName);
  463.         return Adesk::kFalse;
  464.         }
  465.     AcDbObjectId refId;
  466.     if(pExtBT->getAt(newName, refId)==Acad::eOk)
  467.         {
  468.         pExtBT->close();
  469.         delete pExtDb;
  470.         acutPrintf("n外部图形有新块名:%s",oldName);
  471.         return Adesk::kFalse;
  472.         }
  473.     pExtBT->close();
  474.     //
  475.     AcDbBlockTableRecord* pExtBTRec;
  476.     if(acdbOpenObject(pExtBTRec,extBTRecId,AcDb::kForWrite)!=Acad::eOk)
  477.         {
  478.         delete pExtDb;
  479.         acutPrintf("n不能打开块!");
  480.         }
  481.     pExtBTRec->setName(newName);
  482.     pExtBTRec->close();
  483.     pExtDb->saveAs(fName);
  484.     delete pExtDb;
  485.     pExtDb=NULL;
  486.     acutPrintf("n块名修改成功!");
  487.     return Adesk::kTrue;
  488.     
  489. }