Inserting block using AutoCAD COM API

By Balaji Ramamoorthy

Here is a sample ObjectARX code to demonstrate the usage of InsertBlock method of the AutoCAD COM API.

#pragma warning( disable : 4278 )
// Makes change to the tlb name based on the AutoCAD version. 
// For ex : acax18enu.tlb for AutoCAD 2010/2011 and 2012
//          acax19enu.tlb for AutoCAD 2013  
#import "acax19ENU.tlb" no_implementation raw_interfaces_only named_guids
#pragma warning( default : 4278 )
 
#include
static void ADSProjectInsertBlock(void)
{
    int ret = RTNORM;
 
    TCHAR drawingFilePath[500];
    drawingFilePath[0] = _T('');
    ret = acedGetString (
                            NULL, 
                            _T("Enter file path : "), 
                            drawingFilePath
                        );
    if(ret != RTNORM)
        return;
 
    ads_point insertionPoint;
    ret = acedGetPoint(
                        NULL, 
                        _T("nEnter insertion point: "),
                        insertionPoint
                       );
    if(ret != RTNORM)
        return;
 
    CWinApp *pApp = acedGetAcadWinApp();
    HRESULT hRes;
    LPDISPATCH pDisp=NULL;
 
    if(!pApp)
        return;
 
    pDisp=pApp->GetIDispatch(TRUE);
    if    (!pDisp)
          return;
 
    CComPtr  pComApp;
    hRes=pDisp->QueryInterface(
                                IID_IAcadApplication, 
                                (void**)&pComApp
                              );
    if (FAILED(hRes))
          return;
 
    CComPtr pComDoc;
    hRes=pComApp->get_ActiveDocument(&pComDoc);
    if(FAILED(hRes))
        return;
 
    CComPtr pMSpace = NULL;
    pComDoc->get_ModelSpace(&pMSpace);
 
    _bstr_t block(drawingFilePath);
    CComPtr pBlkRef = NULL;
 
    SAFEARRAYBOUND rgsaBound;
    rgsaBound.lLbound = 0L;
    rgsaBound.cElements = 3;
    SAFEARRAY* pInsertionPoint = NULL;
    pInsertionPoint = SafeArrayCreate(VT_R8, 1, &rgsaBound);
 
    for(long i = 0; i < 3; i++)
    {
        double value = insertionPoint[i];
        SafeArrayPutElement(
                                pInsertionPoint, 
                                &i, 
                                &value
                            );
    }
 
    VARIANT vInsertionPoint;
    VariantInit(&vInsertionPoint);
    V_VT(&vInsertionPoint) = VT_ARRAY | VT_R8;
    V_ARRAY(&vInsertionPoint) = pInsertionPoint;
 
    double scaleX = 1.0;
    double scaleY = 1.0;
    double scaleZ = 1.0;
    double rotation = 0.0;
 
    pMSpace->InsertBlock(
                            vInsertionPoint, 
                            block, 
                            scaleX, 
                            scaleY, 
                            scaleZ, 
                            rotation, 
                            vtMissing, 
                            &pBlkRef
                        );
 
    VariantClear(&vInsertionPoint);
}

Comments

One response to “Inserting block using AutoCAD COM API”

  1. petcon Avatar
    petcon

    i think there is a way not using com,show me

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading