• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    Autodesk ObjectARX

    Reply
    Active Member
    Posts: 10
    Registered: ‎05-14-2008
    Accepted Solution

    Help Me Insert Block

    200 Views, 7 Replies
    11-21-2012 12:10 AM

    Hi everyone!

    I'm coding for Insert Block from file DWG to Current Drawing autocad. But error.

    anyone Modify my code

    Thanks

    void Block()
    {
    ACHAR* szName = _T("luanuoc");
    ACHAR *fileName = _T("C:\\procad\\dwg\\luanuoc.dwg");
    AcDbDatabase *pDb = acdbHostApplicationServices()->workingDatabase();
    AcDbDatabase *pBlockDb = new AcDbDatabase(false,true);
    pBlockDb ->readDwgFile(fileName,_SH_DENYRW,true);
    AcDbBlockTable *BlkTable;
    AcDbObjectId BlkId;
    //AcDbObjectIdArray Ids = new AcDbObjectIdArray();
    //BlkId =pBlockDb ->blockTableId();
    BlkTable ->getAt(ACDB_MODEL_SPACE,BlkId,AcDb::kForRead);
    pDb->insert(BlkId,szName,pBlockDb,true);
    if (BlkTable->has(BlkId))
    {
    ads_point p;

    acedGetPoint(NULL,"Chon diem chen",p);
    AcGePoint3d Inspoint(p[X],p[Y],p[Z]);
    AcDbBlockReference *pBlockobj = new AcDbBlockReference;
    AcDbBlockTableRecord *pBlkTablerec;
    pBlockobj ->setBlockTableRecord(BlkId);
    pBlockobj ->setPosition(Inspoint);
    pBlockobj ->setScaleFactors(1);
    BlkTable ->getAt(ACDB_MODEL_SPACE,pBlkTablerec,AcDb::kForWrite);
    BlkTable ->close();
    pBlkTablerec ->appendAcDbEntity(pBlockobj);
    pBlkTablerec ->close();
    pBlockobj ->close();


    }
    }

    Please use plain text.
    Mentor
    Posts: 249
    Registered: ‎08-06-2002

    Re: Help Me Insert Block

    11-21-2012 06:18 AM in reply to: xuanhuynh

    What is the error?

    --
    Owen Wengerd
    ManuSoft
    Please use plain text.
    Active Member
    Posts: 10
    Registered: ‎05-14-2008

    Re: Help Me Insert Block

    11-21-2012 06:18 PM in reply to: owenwengerd

    Error Insert Block. Can you help me?

    Please use plain text.
    Mentor
    Posts: 249
    Registered: ‎08-06-2002

    Re: Help Me Insert Block

    11-21-2012 07:50 PM in reply to: xuanhuynh

    Sorry, I don't understand what the problem is.

    --
    Owen Wengerd
    ManuSoft
    Please use plain text.
    Moderator
    Alexander.Rivilis
    Posts: 1,178
    Registered: ‎04-09-2008

    Re: Help Me Insert Block

    11-22-2012 02:40 AM in reply to: xuanhuynh

    xuanhuynh wrote:

    Error Insert Block. Can you help me?


    Owen asked you about error code. IMHO it is a Fatal Error because of using non-initialize variable BlkTable in line:

    BlkTable ->getAt(ACDB_MODEL_SPACE,BlkId,AcDb::kForRead);

     


    Пожалуйста не забывайте про Утвердить в качестве решения!Утвердить в качестве решения и Give Kudos!Баллы
    Please remember to Accept Solution!Accept as Solution and Give Kudos!Kudos

    Please use plain text.
    Active Member
    Posts: 10
    Registered: ‎05-14-2008

    Re: Help Me Insert Block

    11-23-2012 08:58 PM in reply to: Alexander.Rivilis

    Thanks, I fixed this error.

    But I have this code for create Block with two Attributes. Then Insert this Block into drawing.

    But erro they missing Attributes when i insertBlock:

    1. I use Function TaoBLKDCC2 for create Block and add to Database( this very good)

    2. Then Use function InsertKH1 for Insert Block with name DCC21 into Drawing with two Attributes( this function error, not have attributes

    Can you help me? thank very much!

    void
    ProcadUtil::TaoBLKDCC2()
    {
    AcDbBlockTable *pBlkTbl;
    acdbHostApplicationServices()->workingDatabase() ->getBlockTable(pBlkTbl, AcDb::kForWrite);
    AcDbBlockTableRecord *pBlkTblRcd;
    pBlkTblRcd = new AcDbBlockTableRecord();
    /*char blkName[40];
    if (acedGetString(Adesk::kFalse, "\nNhap ten Block:", blkName) != RTNORM)
    {
    pBlkTbl->close();
    delete pBlkTblRcd;
    return;
    } */
    pBlkTblRcd->setName("DCC21");
    AcDbObjectId blkDefId;
    pBlkTbl->add(blkDefId, pBlkTblRcd);
    pBlkTbl->close();
    AcGePoint3d ptStart, ptEnd;

    ptStart =ProcadUtil::smileytongue:olarPoint(AcGePoint3d::kOrigin,3*ProcadUtil::smileytongue:I()/4,1.41421);
    ptEnd =ProcadUtil::smileytongue:olarPoint(ptStart,0,2);
    AcDbLine *pLine1 = new AcDbLine(ptStart, ptEnd);

    ptStart =ProcadUtil::smileytongue:olarPoint(ptEnd,3*ProcadUtil::smileytongue:I()/2,2);
    AcDbLine *pLine2 = new AcDbLine(ptEnd, ptStart);

    ptEnd =ProcadUtil::smileytongue:olarPoint(ptStart,ProcadUtil::smileytongue:I(),2);
    AcDbLine *pLine3 = new AcDbLine(ptStart, ptEnd);

    ptStart =ProcadUtil::smileytongue:olarPoint(ptEnd,ProcadUtil::smileytongue:I()/2,2);
    AcDbLine *pLine4 = new AcDbLine(ptEnd, ptStart);

    ptStart=ProcadUtil::smileytongue:olarPoint(AcGePoint3d::kOrigin,0,1);
    ptEnd =ProcadUtil::smileytongue:olarPoint(ptStart,0,0.68);
    AcDbLine *pLine5 = new AcDbLine(ptStart, ptEnd);

    ptStart=ProcadUtil::smileytongue:olarPoint(AcGePoint3d::kOrigin,ProcadUtil::smileytongue:I()/2,1);
    ptEnd =ProcadUtil::smileytongue:olarPoint(ptStart,ProcadUtil::smileytongue:I()/2,0.68);
    AcDbLine *pLine6 = new AcDbLine(ptStart, ptEnd);

    ptStart=ProcadUtil::smileytongue:olarPoint(AcGePoint3d::kOrigin,ProcadUtil::smileytongue:I(),1);
    ptEnd =ProcadUtil::smileytongue:olarPoint(ptStart,ProcadUtil::smileytongue:I(),0.68);
    AcDbLine *pLine7 = new AcDbLine(ptStart, ptEnd);

    ptStart=ProcadUtil::smileytongue:olarPoint(AcGePoint3d::kOrigin,3*ProcadUtil::smileytongue:I()/2,1);
    ptEnd =ProcadUtil::smileytongue:olarPoint(ptStart,3*ProcadUtil::smileytongue:I()/2,0.68);
    AcDbLine *pLine8 = new AcDbLine(ptStart, ptEnd);

    ptStart=ProcadUtil::smileytongue:olarPoint(AcGePoint3d::kOrigin,0,2.68);
    ptEnd =ProcadUtil::smileytongue:olarPoint(ptStart,0,8);
    AcDbLine *pLine9 = new AcDbLine(ptStart, ptEnd);
    AcGeVector3d vecNormal(0, 0, 1);
    AcDbCircle *pCircle = new AcDbCircle(AcGePoint3d::kOrigin,vecNormal, 0.1);
    AcGePoint3d BasePoint = ProcadUtil::asAc3d(ptStart);
    AcDbAttributeDefinition *pAttDef1 = new AcDbAttributeDefinition;

    //pAttDef1->setPosition(ProcadUtil::smileytongue:olarPoint(ptStart,ProcadUtil::smileytongue:I()/2,0.5));
    ptStart = ProcadUtil::smileytongue:olarPoint(BasePoint,ProcadUtil::smileytongue:I()/2,0.5);
    pAttDef1->setPosition(ptStart);
    pAttDef1->setPrompt("TENDIEM");
    pAttDef1->setTag("TENDIEM");
    //pAttDef1->setTextStyle(_T("chudcc2"));
    pAttDef1->setHeight(2);
    //pAttDef1->setAlignmentPoint()
    pAttDef1->setHorizontalMode(AcDb::TextHorzMode::kTextLeft);
    pAttDef1->setVerticalMode(AcDb::TextVertMode::kTextBase);
    pAttDef1->setLayer("TenDiem");

    AcDbAttributeDefinition *pAttDef2 = AcDbAttributeDefinition::cast(pAttDef1->clone()) ;
    //pAttDef1->setPosition(ProcadUtil::smileytongue:olarPoint(ptStart,ProcadUtil::smileytongue:I()/2,0.5));
    ptStart = ProcadUtil::smileytongue:olarPoint(BasePoint,3*ProcadUtil::smileytongue:I()/2,2.5);
    pAttDef2->setPosition(ptStart);
    pAttDef2->setPrompt("DOCAO");
    pAttDef2->setTag("DOCAO");
    //pAttDef1->setTextStyle("chudcc2");
    pAttDef2->setHeight(2);
    //pAttDef1->setAlignmentPoint()
    pAttDef2->setHorizontalMode(AcDb::TextHorzMode::kTextLeft);
    pAttDef2->setVerticalMode(AcDb::TextVertMode::kTextBase);
    pAttDef2->setLayer("DoCao");

    AcDbObjectId entId;
    pBlkTblRcd->appendAcDbEntity(entId, pLine1);
    pBlkTblRcd->appendAcDbEntity(entId, pLine2);
    pBlkTblRcd->appendAcDbEntity(entId, pLine3);
    pBlkTblRcd->appendAcDbEntity(entId, pLine4);
    pBlkTblRcd->appendAcDbEntity(entId, pLine5);
    pBlkTblRcd->appendAcDbEntity(entId, pLine6);
    pBlkTblRcd->appendAcDbEntity(entId, pLine7);
    pBlkTblRcd->appendAcDbEntity(entId, pLine8);
    pBlkTblRcd->appendAcDbEntity(entId, pLine9);
    pBlkTblRcd->appendAcDbEntity(entId, pCircle);
    pBlkTblRcd->appendAcDbEntity(entId, pAttDef1);
    pBlkTblRcd->appendAcDbEntity(entId, pAttDef2);
    pLine1->close();
    pLine2->close();
    pLine3->close();
    pLine4->close();
    pLine5->close();
    pLine6->close();
    pLine7->close();
    pLine8->close();
    pLine9->close();
    pCircle->close();
    pAttDef1->close();
    pAttDef2->close();
    pBlkTblRcd->close();
    }
    //Ham convert diem ads_point -> AcGePoint3d
    AcGePoint3d
    ProcadUtil::asPnt3d(ads_point &pt)
    {
    AcGePoint3d pInsert;
    pInsert[X] = pt[X];
    pInsert[Y] = pt[Y];
    pInsert[Z] = pt[Z];
    return(pInsert);
    }
    //-------------------------
    AcGePoint3d
    ProcadUtil::smileytongue:olarPoint(const AcGePoint3d& pt, double angle,double distance)
    {
    ads_point ptForm, ptTo;
    ptForm[X] = pt.x;
    ptForm[Y] = pt.y;
    ptForm[Z] = pt.z;
    acutPolar(ptForm, angle, distance, ptTo);
    return ProcadUtil::asPnt3d(ptTo);
    }
    double
    ProcadUtil::smileytongue:I()
    {
    return 4 * atan(1.0);
    }
    AcGePoint3d
    ProcadUtil::asAc3d(const AcGePoint3d& pt)
    {
    AcGePoint3d p1;
    p1[X]=pt[X];
    p1[Y]=pt[Y];
    p1[Z]=pt[Z];
    return p1 ;
    }

    void
    ProcadUtil::InsertKH1(TCHAR *szBlock, TCHAR *szDwg, double x, double y, double elev,int stt, double Tile,double goc)
    {
    AcGePoint3d Inspoint(x,y,elev);
    AcDbBlockTable *pBlkTable=NULL;
    AcDbBlockTableRecord *pBlkTablerec;
    AcDbDatabase *pCurDb = NULL;
    AcGeScale3d TileScale(Tile,Tile,Tile);
    pCurDb = acdbHostApplicationServices()->workingDatabase();
    pCurDb ->getSymbolTable(pBlkTable,AcDb::kForRead);
    AcDbObjectId BlkId;
    pBlkTable ->getAt(szBlock,BlkId);
    if(pBlkTable->has(szBlock))
    {
    AcDbBlockReference *pBlockobj = new AcDbBlockReference;
    pBlockobj ->setBlockTableRecord(BlkId);
    pBlockobj ->setPosition(Inspoint);
    pBlockobj ->setScaleFactors(Tile);
    pBlkTable ->getAt(ACDB_MODEL_SPACE,pBlkTablerec,AcDb::kForWrite);
    pBlkTable ->close();
    AcDbObjectId pEntId;
    pBlkTablerec ->appendAcDbEntity(pEntId,pBlockobj);
    pBlkTablerec ->close();
    AcDbObjectIterator *pBlkRefAttItr=pBlockobj->attributeIterator();
    for (pBlkRefAttItr->start(); !pBlkRefAttItr->done();pBlkRefAttItr->step())
    {
    AcDbObjectId attObjId;
    attObjId = pBlkRefAttItr->objectId();
    AcDbAttribute *pAtt = NULL;
    Acad::ErrorStatus es = acdbOpenObject(pAtt, attObjId, AcDb::kForRead);
    if (es != Acad::eOk)
    {
    acutPrintf(_T("\nLoi khong mo duoc attribute"));
    delete pBlkRefAttItr;
    continue;
    }
    if (strcmp(pAtt->tag(),"TENDIEM") == 0)
    {
    pAtt->setTextString("DC12");
    }
    if (strcmp(pAtt->tag(),"DOCAO:") == 0)
    {
    pAtt->setTextString("12.63");
    }
    pBlockobj->appendAttribute(pAtt);
    pAtt->close();
    }
    delete pBlkRefAttItr;
    pBlockobj->close();
    }
    }

     

    Please use plain text.
    Moderator
    Alexander.Rivilis
    Posts: 1,178
    Registered: ‎04-09-2008

    Re: Help Me Insert Block

    11-24-2012 10:24 AM in reply to: xuanhuynh
    You forgot after creation AcDbBlockReference add all AcDbAttribute's to it (with help of AcDbAttribute::setAttributeFromBlock method)

    Пожалуйста не забывайте про Утвердить в качестве решения!Утвердить в качестве решения и Give Kudos!Баллы
    Please remember to Accept Solution!Accept as Solution and Give Kudos!Kudos

    Please use plain text.
    Active Member
    Posts: 10
    Registered: ‎05-14-2008

    Re: Help Me Insert Block

    11-25-2012 07:46 AM in reply to: xuanhuynh

    I Fixed this error.

    Thank you very much.

    Please use plain text.