明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2202|回复: 1

[求助]如何获取用insert插入的属性块的各属性值(块名已知)

[复制链接]
发表于 2005-8-22 10:09:00 | 显示全部楼层 |阅读模式
如何获取用insert插入的属性块的各属性值(块名已知)
发表于 2005-8-22 10:47:00 | 显示全部楼层

这是一本书中的例子,每一行代码的例子我还不太懂,你自己看看吧.其中包括插入块的代码.

void insrtBlk()
{
 char blkName[50];
 AcDbDatabase *pCurDb;
 AcDbBlockTable *pBlkTable;
 AcDbBlockTableRecord *pBlkTableRecord;
 AcDbBlockTableRecord *pBlkDefRecord;
 AcDbBlockReference *pInsrtObj;
 AcDbEntity *pEnt;
 AcDbBlockTableRecordIterator *pIterator;
 AcDbAttributeDefinition *pAttDef;
 AcDbAttribute *pAtt;
 AcDbObjectId blkId;
 AcDbObjectId insrtId;

 char *pTagPrompt;

 AcGePoint3d insPt;
 AcGePoint3d basePt;

 int retCode;

 retCode = acedGetString(0, "\nEnter Block Name: ", blkName);
 if(retCode != RTNORM || blkName[0] == '\0')
 {
  acutPrintf("\nInvalid block name.");
  return;
 }

 pCurDb = acdbHostApplicationServices()->workingDatabase();

 // Check to see if the block table
 // has blkName

 pCurDb->getBlockTable(pBlkTable, AcDb::kForRead);
 if(!pBlkTable->has(blkName))
 {
  acutPrintf("\nBlock definition %s not found. ", blkName);
  pBlkTable->close();
  return;
 }

 // Get the AcDbObjectId of the block
 // definition.
 pBlkTable->getAt(blkName, blkId);

 pBlkTable->getAt(ACDB_MODEL_SPACE, pBlkTableRecord, AcDb::kForWrite);
 pBlkTable->close();
 
 acedInitGet(RSG_NONULL, NULL);
 acedGetPoint(NULL, "\nPick insertion point: ", asDblArray(insPt));

 pInsrtObj = new AcDbBlockReference(insPt, blkId);

 // Here is where you can set scale, rotation and other
 // properties to the block entity. If you want to
 // see the AcDbBlockReference class for more details.
 pBlkTableRecord->appendAcDbEntity(insrtId, pInsrtObj);


 acdbOpenObject(pBlkDefRecord, blkId, AcDb::kForRead);
 // Now check to see if the Block Definition
 // has attributes. If it does we will add
 // a Block Table Record Iterator to step through
 // the entities and find the Attribute Definitions.

 if(pBlkDefRecord->hasAttributeDefinitions())
 {
  pBlkDefRecord->newIterator(pIterator);

  for(pIterator->start(); !pIterator->done(); pIterator->step())
  {
   pIterator->getEntity(pEnt, AcDb::kForRead);
   // Check to see if the entity is an
   // attribute definition.
   pAttDef = AcDbAttributeDefinition::cast(pEnt);
   if(pAttDef != NULL && !pAttDef->isConstant())
   {
    // If it is and its not constant
    // create a new attribute
    pAtt = new AcDbAttribute();
    // setPropertiesFrom will copy
    // Color, Layer, Linetype,Linetype scale and
    // Visibility.
    pAtt->setPropertiesFrom(pAttDef);
    // setup more properties from the attribute
    // definition
    pAtt->setInvisible(pAttDef->isInvisible());
    basePt = pAttDef->position();
    basePt += pInsrtObj->position().asVector();
    pAtt->setPosition(basePt);
    pAtt->setHeight(pAttDef->height());
    pAtt->setRotation(pAttDef->rotation());

    // Take note how we get the tag.
    pTagPrompt = pAttDef->tag();
    pAtt->setTag(pTagPrompt);
    free(pTagPrompt);

    // Normally you would prompt the user
    // and ask for input values.
    pTagPrompt = pAttDef->prompt();
    acutPrintf("%s%s", "\n", pTagPrompt);
    free(pTagPrompt);
   
    // The setFieldLength is not required
    // even though it is listed in the
    // documentation.
    pAtt->setFieldLength(25);
    // setTextString is the value the
    // attribute receives which would
    // normally be a user input value.
    pAtt->setTextString("This is a test");

    pInsrtObj->appendAttribute(pAtt);
    pAtt->close();
   }
   pEnt->close();
  }// for
  delete pIterator;

}// if has attribute definitions

  // Note that we close the Model Space
 // block table record after we have added
 // our attributes.
 pBlkTableRecord->close();
 pInsrtObj->close();
}

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-26 06:49 , Processed in 0.156147 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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