从C在AutoCAD中绘制一个圆

时间:2017-01-22 16:54:45

标签: c autocad

我正在尝试在已经运行的AutoCAD实例中绘制一个圆圈,将其附加到现有绘图中。

这可能吗?什么是最简单的方法?

THX

3 个答案:

答案 0 :(得分:1)

如您所述"已经运行的AutoCAD"的实例,我假设您想要自动化AutoCAD,通常是通过ActiveX COM Automation。您应该使用AcadApplication接口(并且不能使用AcDb或.NET Mgd引用)。

这是来自this blog post的ActiveX C ++通用代码示例。请注意acac19enu.tlb参考,其中19代表AutoCAD版本。目前的是AutoCAD 2017(库版本:21)。

#import "acax19ENU.tlb" no_namespace
#include <rxmfcapi.h>
#include <axpnt3d.h>
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());
  }
}

答案 1 :(得分:0)

在C中找不到任何内容,但这是Developer Guide中的C#示例:

var stock_position = 10;// set by PHP

function showCost(shares){
    var cost = stock_position * shares;
    $("#cost").text(cost);
}
$(function() {
    var $list = $("#shares");
    $list.change(function(){
        showCost($(this).val());
    });
    showCost($list.val());
});

答案 2 :(得分:0)

在纯C中它可能很复杂,所以祝你好运。 但是如果你使用C ++和ARX试试这个:

Acad::ErrorStatus AddCircle(AcGePoint3d center , double r , AcDbObjectId& id , AcDbBlockTableRecord* pBlock)
{
    AcDbCircle* pCirEnt = NULL;
    pCirEnt  = new AcDbCircle();
    pCirEnt->setCenter(center);
    pCirEnt->setRadius(r);
    Acad::ErrorStatus es = Add(pCirEnt , pBlock);
    es = pCirEnt->close();
    id = pCirEnt->objectId();
    return es ;
}


Acad::ErrorStatus Add( AcDbEntity * pEnt, AcDbBlockTableRecord* Blok)
{
    if ( !pEnt )    {       
        return Acad::eNullEntityPointer  ;
    }

    Acad::ErrorStatus es;
    if (!Blok)
    {
        Blok = getModelSpace(AcDb::kForWrite);
        if (!Blok) return Acad::eInvalidOwnerObject;
    }
    if (Blok->isWriteEnabled() == Adesk::kFalse )
    {
        AcDbObject* pObj = NULL;    
        es = acdbOpenObject(pObj,Blok->objectId (),AcDb::kForWrite) ;
        Blok = AcDbBlockTableRecord::cast(pObj);
    }
    if ((es = Blok->appendAcDbEntity(pEnt)) != Acad::eOk )  // eAlreadyInDb = wcześniej wstawione do innego bloku.
    {
        Blok->close();
        return es;
    }
    Blok->close();
    return Acad::eOk;
}

AcDbBlockTableRecord* getModelSpace(AcDb::OpenMode mode)
{
    AcDbBlockTableRecord* Blok = NULL;
    Acad::ErrorStatus es;
    AcDbDatabase * pDb = acdbHostApplicationServices()->workingDatabase();
    if (!pDb) return NULL;
    AcDbBlockTable* pTbl = NULL;
    if ((es= pDb->getBlockTable(pTbl, AcDb::kForRead) ) != Acad::eOk  )
        return NULL;
    if ((es = pTbl->getAt(ACDB_MODEL_SPACE, Blok, mode)) !=  Acad::eOk )
    {
        pTbl->close();
        return NULL;
    }
    pTbl->close();

    return Blok;
}