|
在 IAcadBlock 中使用 ActiveX 迭代每个 IAcadBlock,C++
问题
在C++,我似乎无法迭代IAcadBlocks来访问每个IAcadBlock。是否有执行此操作的示例代码?
溶液
以下两种方法迭代 IAcadBlocks 以访问每个 IAcadBloc。这假定您已经设置了骨架 ARX/MFC 项目,并导入了 AutoCAD COM 库的 tlb。
[code]void test()
{
try
{
// get application
IAcadApplicationPtr pIAcad =
acedGetAcadWinApp()->GetIDispatch(TRUE);
// get blocks collection
IAcadBlocksPtr pIBlks =
pIAcad->GetActiveDocument()->GetBlocks();
// count of blocks
long nCnt =pIBlks->GetCount();
for(long i=0; i<nCnt; i++)
{
_variant_t var(i);
// each block
IAcadBlockPtr pIBlk =
pIBlks->Item(var);
BSTR str;
// get block name
pIBlk->get_Name(&str);
_bstr_t bstr(str);
// pop out the block name
AfxMessageBox(bstr);
::SysFreeString(str);
}
}
catch(_com_error& err)
{
AfxMessageBox(err.ErrorMessage());
}
}
If you want to use the #import generated files that come ObjectARX, do it as the following code shows:
static void test()
{
try
{
// get application
IDispatch* pDisp =
acedGetAcadWinApp()->GetIDispatch(TRUE);
if(!pDisp) _com_issue_error(E_POINTER);
CComPtr<IAcadApplication> pIAcad;
pDisp->QueryInterface(IID_IAcadApplication, (void**)&pIAcad);
// get current document
CComPtr<IAcadDocument> pIDoc;
pIAcad->get_ActiveDocument(&pIDoc);
// get blocks collection
CComPtr<IAcadBlocks> pIBlks;
pIDoc->get_Blocks(&pIBlks);
long blksCnt;
pIBlks->get_Count(&blksCnt);
CComPtr<IAcadBlock> pIBlk;
// iterate each block
for(long i=0; i<blksCnt; i++)
{
_variant_t x(i);
pIBlks->Item(x, &pIBlk);
BSTR str;
pIBlk->get_Name(&str);
char buf[256];
buf [0] =0 ;
if(WideCharToMultiByte (CP_ACP,
0,
str,
-1,
buf,
256,
NULL,
NULL))
acutPrintf(_T("\nBlock name %d is: %s"), i, buf);
::SysFreeString(str);
}
}
catch(_com_error& err)
{
AfxMessageBox(err.ErrorMessage());
}
}[/code] |
|