- 积分
- 3266
- 明经币
- 个
- 注册时间
- 2002-7-5
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2003-1-15 09:13:00
|
显示全部楼层
给段代码你自己参考参考
如果你要插入外部图块,也就是说,要将另一张DWG图形插入,建议使用AcDbDatabase::insert()函数,以下代码用于插入AcDbBlockReference对象,即块参考。
void addBlockWithAttributes()
{
AcGePoint3d basePoint; //插入基点
if(acedGetPoint(NULL,"\n请输入图块插入点:",asDblArray(basePoint))!= RTNORM)
return;
double textAngle; //属性文字旋转角度
if(acedGetAngle(asDblArray(basePoint),"\n属性文字旋转角度:", &textAngle)!= RTNORM)
return;
double textHeight; //属性文字高度
if(acedGetDist(asDblArray(basePoint),"\n属性文字高度:", textHeight)!= RTNORM)
return;
// 定义要插入的图块ID
AcDbObjectId blockId;
defineBlockWithAttributes(blockId, basePoint,textHeight, textAngle);
// 第一步,给新的块参考对象分配内存
AcDbBlockReference *pBlkRef = new AcDbBlockReference;
// 第二步,定义好的图块ID赋给块参考对象
pBlkRef->setBlockTableRecord(blockId);
// 若使用UCS,建立UCS与WCS之间的转换
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));
// 设置块参考对象的插入点
pBlkRef->setPosition(basePoint);
// 指定LCS角度为0.
pBlkRef->setRotation(0.0);
pBlkRef->setNormal(normal);
// 第三步,打开当前AcDb数据库模型空间块表记录
AcDbBlockTable *pBlockTable;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBlockTable, AcDb::kForRead);
AcDbBlockTableRecord *pBlockTableRecord;
pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite);
pBlockTable->close();
// 向模型空间添加块参考对象
AcDbObjectId newEntId;
pBlockTableRecord->appendAcDbEntity(newEntId, pBlkRef);
pBlockTableRecord->close();
//以上是新建块参考对象的方法,以下读出块表并设置属性值
// 第四步,打开块表记录
AcDbBlockTableRecord *pBlockDef;
acdbOpenObject(pBlockDef, blockId, AcDb::kForRead);
// 建立块表记录浏览器,遍历块表
AcDbBlockTableRecordIterator *pIterator;
pBlockDef->newIterator(pIterator);
AcDbEntity *pEnt;
AcDbAttributeDefinition *pAttdef;
for (pIterator->start(); !pIterator->done();pIterator->step())
{
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);
free(pStr);
pAtt->setFieldLength(pAttdef->fieldLength());
// 输入属性值
pAtt->setTextString("Assigned Attribute Value");
AcDbObjectId attId;
pBlkRef->appendAttribute(attId, pAtt);
pAtt->close();
}
pEnt->close(); // 不要忘记释放内存
}
delete pIterator;
pBlockDef->close();
pBlkRef->close();
} |
|