明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1970|回复: 3

to 版主

[复制链接]
发表于 2003-4-19 22:07:00 | 显示全部楼层 |阅读模式
前段时间有篇名叫《编程日记(1)ARX中如何insert一个图》的文章怎么没看见了??
还有我按照上面的思路写了一段程序,但是插入后,AutoCAD当前窗口中没有我所插入的图形!请指教!!


程序代码如下:
void InsertBlock()
{


        char bName[256],pfName[256];
        int es;
        AcGePoint3d Pt(0,0,0);
        double Angle = 0.0;
        AcGeScale3d XrefScale(1,1,1);

        strcpy(bName,"first");
        strcpy(pfName,"F:\\FENGH\\program\\AutoCAD\\ObjectARX\\LibMag\\libmag.dwg");
       


       
        AcDbDatabase *pDbMS = new AcDbDatabase(Adesk::kFalse);


        if(pDbMS->readDwgFile(pfName) != Acad::eOk)
        {
                acutPrintf("\nThe file %s cannot be opend",pfName);
                return;


        }
//        pDbMS->getBlockTable(pBlockTable,AcDb::kForRead);
//        pBlockTable->close();       
       
        AcDbDatabase * curpDb = acdbHostApplicationServices()->workingDatabase();
        AcDbObjectId blockId;
       
        if((es = curpDb->insert(blockId, bName,pDbMS, true)) == Acad::eOk) //´ÓÊý¾Ý¿â²åÈëͼ¿âÖÐ
        {
                acutPrintf("\ninsert ok\n");
               
        }
        else
        {
                AfxMessageBox("Insert failed");
                delete pDbMS;
                return;
        }

        AcDbBlockReference *pBlkRef = new AcDbBlockReference; //´´½¨Ò»¸ö¿é±íÒýÓÃ
        pBlkRef->setBlockTableRecord(blockId);// Ö¸ÏòblockId;

        pBlkRef->setPosition(Pt);//ÉèÖÃλÖÃ
        pBlkRef->setRotation(Angle);//ÉèÖÃת½Ç
        pBlkRef->setScaleFactors( XrefScale);//ÉèÖ÷Ŵó±ÈÀý



        AcDbBlockTable *pBlockTable;
        curpDb->getSymbolTable(pBlockTable, AcDb::kForRead);

        AcDbBlockTableRecord *pBlockTableRecord;
        pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite);
        pBlockTable->close();
        AcDbObjectId newEntId;
        pBlockTableRecord->appendAcDbEntity(newEntId, pBlkRef);
        pBlockTableRecord->close();
       
//        delete pDbMS;       
        acutPrintf("\nprogram begin add attribute\n");

        AcDbBlockTableRecord *pBlockDef;
        acdbOpenObject(pBlockDef, blockId, AcDb::kForRead);

        AcDbBlockTableRecordIterator *pIterator;
        pBlockDef->newIterator(pIterator);
        AcGePoint3d basePoint;
        AcDbEntity *pEnt;
        AcDbAttributeDefinition *pAttdef;
        for (pIterator->start(); !pIterator->done();
        pIterator->step())//½«source.dwgÖеÄËùÓÐAttibute½øÐбéÀú
        {
        pIterator->getEntity(pEnt, AcDb::kForRead);
        pAttdef = AcDbAttributeDefinition::cast(pEnt);
        
                if (pAttdef != NULL && !pAttdef->isConstant())
                {
            AcDbAttribute *pAtt = new AcDbAttribute();
            pAtt->setPropertiesFrom(pAttdef);
            pAtt->setInvisible(pAttdef->isInvisible());
            basePoint = pAttdef->position();
            basePoint += pBlkRef->position().asVector();
            pAtt->setPosition(basePoint);
            pAtt->setHeight(pAttdef->height());
            pAtt->setRotation(pAttdef->rotation());
            pAtt->setTag("Tag");
            pAtt->setFieldLength(25);
            char *pStr = pAttdef->tag();
            pAtt->setTag(pStr);
                        acutDelString(pStr);
            pAtt->setFieldLength(pAttdef->fieldLength());
            pAtt->setTextString("-");

            AcDbObjectId attId;

            pBlkRef->appendAttribute(attId, pAtt);
            pAtt->close();
        }
        pEnt->close(); // use pEnt... pAttdef might be NULL
    }
    pBlockDef->close();
        delete pIterator;
        delete pDbMS;       
        acutPrintf("\nprogram end!\n");


}
发表于 2003-4-21 08:09:00 | 显示全部楼层

把你的代码和下面你码对照,看看有何不同?

//插入图形文件
Acad::ErrorStatus InsertDwgFile(CString strFilename,CString strBlockname)
{
        Acad::ErrorStatus es;
        AcDbDatabase *pDb = new AcDbDatabase(Adesk::kFalse);
        pDb->readDwgFile(strFilename);
        AcDbObjectId Id;
        es = acdbHostApplicationServices()->workingDatabase()->insert(Id,strBlockname,pDb);
        delete pDb;
        return es;
}
//////////////////////////////////////////////////////////////
void InsertBlockWithAttributes(CString strBlockname,AcGePoint3d basePoint,double dAngle,AcGeScale3d scale)
{
    // Build the block definition to be inserted.
    AcDbObjectId blockId;
        AcDbBlockTable *pBlockTable1;
          acdbHostApplicationServices()->workingDatabase()
        ->getSymbolTable(pBlockTable1, AcDb::kForWrite);
          pBlockTable1->getAt(strBlockname,blockId);
          pBlockTable1->close();

    // Step 1: Allocate a block reference object.
    AcDbBlockReference *pBlkRef = new AcDbBlockReference;

    // Step 2: Set up the block reference to the newly
    // created block definition.
    pBlkRef->setBlockTableRecord(blockId);

    // Give it the current UCS normal.
    struct resbuf to, from;

    from.restype = RTSHORT;
    from.resval.rint = 1; // UCS
    to.restype = RTSHORT;
    to.resval.rint = 0; // WCS

    AcGeVector3d normal(0.0, 0.0, 1.0);
    acedTrans(&(normal.x), &from, &to, Adesk::kTrue,&(normal.x));

    // Set the insertion point for the block reference.
    pBlkRef->setPosition(basePoint);

    // Indicate the LCS 0.0 angle, not necessarily the UCS 0.0 angle.
    pBlkRef->setRotation(dAngle);
        pBlkRef->setScaleFactors(scale);
    pBlkRef->setNormal(normal);

    // Step 3: Open the current database's model space
    // block Table Record.
    AcDbBlockTable *pBlockTable;
    acdbHostApplicationServices()->workingDatabase()
        ->getSymbolTable(pBlockTable, AcDb::kForRead);

    AcDbBlockTableRecord *pBlockTableRecord;
    pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord,
        AcDb::kForWrite);

    pBlockTable->close();

    // Append the block reference to the model space
    // block Table Record.
    AcDbObjectId newEntId;
    pBlockTableRecord->appendAcDbEntity(newEntId, pBlkRef);
    pBlockTableRecord->close();

    // Step 4: Open the block definition for read.
    AcDbBlockTableRecord *pBlockDef;
    acdbOpenObject(pBlockDef, blockId, AcDb::kForRead);

    // Set up a block table record iterator to iterate
    // over the attribute definitions.
    AcDbBlockTableRecordIterator *pIterator;
    pBlockDef->newIterator(pIterator);

    AcDbEntity *pEnt;
    AcDbAttributeDefinition *pAttdef;
    for (pIterator->start(); !pIterator->done();
        pIterator->step())
    {
        // Get the next entity.
        pIterator->getEntity(pEnt, AcDb::kForRead);

        // Make sure the entity is an attribute definition
        // and not a constant.
        pAttdef = AcDbAttributeDefinition::cast(pEnt);

        if (pAttdef != NULL && !pAttdef->isConstant())
                {
            // We have a non-constant attribute definition,
            // so build an attribute entity.
            AcDbAttribute *pAtt = new AcDbAttribute();
            pAtt->setPropertiesFrom(pAttdef);
            pAtt->setInvisible(pAttdef->isInvisible());

            // Translate the attribute by block reference.
            // To be really correct, the entire block
            // reference transform should be applied here.
            basePoint = pAttdef->position();
            basePoint += pBlkRef->position().asVector();
            pAtt->setPosition(basePoint);

            pAtt->setHeight(pAttdef->height());
            pAtt->setRotation(pAttdef->rotation());

            pAtt->setTag("Tag");
            pAtt->setFieldLength(25);

            char *pStr = pAttdef->tag();
            pAtt->setTag(pStr);
            free(pStr);

            pAtt->setFieldLength(pAttdef->fieldLength());

            // The database column value should be displayed.
            // INSERT prompts for this.
            pAtt->setTextString("Assigned Attribute Value");

            AcDbObjectId attId;

            pBlkRef->appendAttribute(attId, pAtt);
            pAtt->close();
        }
        pEnt->close(); // use pEnt... pAttdef might be NULL
    }
    delete pIterator;
    pBlockDef->close();
    pBlkRef->close();
}

//////////////////////////////////////////
if(InsertDwgFile(strPath + "Block\\a0-0.dwg","A00") != Acad::eOk)
                                {
                                        MessageBox("插入图形文件A0-0.Dwg不成功!");
                                        return;
                                }
AcGePoint3d basePoint(-25,-10,0);
                                AcGeScale3d scale(1,1,1);
                                InsertBlockWithAttributes("A00",basePoint,0.0,scale);
发表于 2003-4-21 09:01:00 | 显示全部楼层

Sorry, 误删,可能是kForRead不对,也可能要锁定文档.

发表于 2003-4-22 11:49:00 | 显示全部楼层

不可以这样

运行InsertDwgFile()后,得到一个ID,创建一个AcDbBlockReference, 并将它指向ID所代表的AcDbBlockRecord, 然后将这个AcDbBlockReference加入pDb所代表的图形数据库中.
你的程序中,得到ID后,没有操作这个ID.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 13:24 , Processed in 0.187486 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表