Adding an Attribute to a Block Definition using C++ COM/ActiveX Automation in ObjectARX

by Fenton Webb

The trick to doing this is to use the I*Ptr interfaces (e.g. IAcadDocumentPtr) so that COM reference counting is handled automatically.

Also, another trick is to stay away from VARIANT’s as much as possible, we have various AcAx* classes which help you do this… Here’s the code…

#import "acax19ENU.tlb" no_namespace     #include     #include     void fAddAttribute()    {      try      {        // get the ActiveX application object from AutoCAD, inc ref count        IAcadApplicationPtr pAcadApp = acedGetAcadWinApp()->GetIDispatch(TRUE);        // now get the active doc        IAcadDocumentPtr pActiveDoc = pAcadApp->ActiveDocument;        IAcadBlockPtr pBlock = NULL;        TCHAR *pBlkName = _T("some_block_name");        // create an activex compatible insertion point3d        AcAxPoint3d axInsPnt(0,0,0);        // now add the block name        pBlock = pActiveDoc->Blocks->Add(axInsPnt.asVariantPtr(),_bstr_t(pBlkName));        // now add an Attribute to the block        IAcadAttributePtr pAttDef;        pAttDef = pBlock->AddAttribute(1.0, (AcAttributeMode)0 ,          _bstr_t("Type the employee name"), axInsPnt.asVariantPtr(),          _bstr_t("empname"),_bstr_t(""));        //attribute added      }      catch(_com_error &es)      {        acutPrintf(L"nError : %s", es.ErrorMessage());      }    }

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading