wwyfeng 发表于 2003-4-19 22:07:00

to 版主

前段时间有篇名叫《编程日记(1)ARX中如何insert一个图》的文章怎么没看见了??
还有我按照上面的思路写了一段程序,但是插入后,autocad当前窗口中没有我所插入的图形!请指教!!


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


        char bName,pfName;
        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");


}

fayifu 发表于 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);

goldenshin 发表于 2003-4-21 09:01:00

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

goldenshin 发表于 2003-4-22 11:49:00

不可以这样

运行InsertDwgFile()后,得到一个ID,创建一个AcDbBlockReference, 并将它指向ID所代表的AcDbBlockRecord, 然后将这个AcDbBlockReference加入pDb所代表的图形数据库中.
你的程序中,得到ID后,没有操作这个ID.
页: [1]
查看完整版本: to 版主