设为首页收藏本站

晓东CAD家园-论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 35|回复: 0

Adding A Block Using ActiveX Client In C++

[复制链接]

已领礼包: 40个

财富等级: 招财进宝

发表于 2021-1-11 20:38:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
问题:
How do I add a block to a drawing through an ActiveX Client in C++?

解答:
There are two interface objects that are mirrored to their ObjectARX
counterparts: IAcadBlocks to AcDbBlockTable and IAcadBlock to
AcDbBlockTableRecord. You then need to get to the interface objects and call the
appropriate methods.

These are the key steps to doing this:

1. Create a standalone MFC AppWiard EXE that is dialog-based.
2. Use the ClassWizard to import the following interfaces (from acad.tlb) and
declare them as private member variables of the dialog class:


IAcadApplication m_IApp;
    IAcadDocument     m_IDoc;

    IAcadBlock    m_IBlock;
    IAcadBlocks    m_IBlocks;

3. In the dialog's OnInitDialog(), assign a separate button handler, and get
the application and document object (a button with handler code shown below).

  1. void CClientDlg::OnStart()
  2. {
  3.     HRESULT hr = NOERROR;
  4.     CLSID clsid;
  5.     LPUNKNOWN pUnk = NULL;
  6.     LPDISPATCH pDisp = NULL;

  7.     BeginWaitCursor();
  8.     CoInitialize(NULL);
  9.     hr = ::CLSIDFromProgID(L"AutoCAD.Application", &clsid);

  10.     if (SUCCEEDED(hr))
  11.     {
  12.         if(::GetActiveObject(clsid, NULL, &pUnk) == S_OK)
  13.         {
  14.             VERIFY(pUnk->QueryInterface(IID_IDispatch,(LPVOID*)&pDisp) == S_OK);
  15.             m_IApp.AttachDispatch(pDisp);
  16.             pUnk->Release();
  17.         }
  18.         else
  19.             VERIFY(m_IApp.CreateDispatch(clsid) == TRUE);

  20.         m_IApp.SetVisible(TRUE);

  21.         pDisp = m_IApp.GetActiveDocument();
  22.         m_IDoc.AttachDispatch(pDisp);
  23.     }
  24.     else
  25.         AfxMessageBox("Acad.Application is not registered!");
  26.             //CoUninitialize(); // do this in the dialog's exit code
  27.     EndWaitCursor();
  28. }

  29. 4. Create a button and its handler, then add the following code:


  30. void CClientDlg::OnAddBlock()
  31. {
  32.     VARIANT var;
  33.     double pt[3] = {2, 2, 0};
  34.     getVariantFromDblArray(&var, pt);
  35.     const char blkName[] = "TEST";
  36.     LPDISPATCH p = m_IDoc.GetBlocks();
  37.     m_IBlocks.AttachDispatch(p);
  38.     p = m_IBlocks.Add(var, blkName);
  39.     m_IBlock.AttachDispatch(p);
  40. }

  41. HRESULT getVariantFromDblArray(VARIANT* pVal, const double pt[3])
  42. {
  43.     pVal->vt = VT_ARRAY | VT_R8;

  44.     SAFEARRAYBOUND rgsaBound;
  45.     rgsaBound.lLbound = 0L;
  46.     rgsaBound.cElements = 3;

  47.     pVal->parray = SafeArrayCreate(VT_R8, 1, &rgsaBound);
  48.     if (! pVal->parray)
  49.         return E_OUTOFMEMORY;

  50.     HRESULT hr;
  51.     for (long i = 0; i < 3; i++)
  52.         if ((hr = SafeArrayPutElement(pVal->parray, &i,
  53. (void*)&pt))!=S_OK)
  54.             return hr;
  55.     return S_OK;
  56. }


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )  

GMT+8, 2021-1-22 08:27 , Processed in 0.079420 second(s), 12 queries , Gzip On, WinCache On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表