//其中blockName为插入的块表的名称, ptInsert为块表指定的插入点
void InsertBlkToSpace(CString blockName, AcGePoint3d ptInsert)
{
//获得当前的数据库的块表
AcDbBlockTable *pBlkTab;
acdbHostApplicationServices()->workingDatabase()
->getBlockTable(pBlkTab, ZcDb::kForWrite);
//判断插入的块定义是否存在
if (!pBlkTab->has(blockName))
{
acutPrintf(_T("\n当前图纸中不包含此块表!"));
pBlkTab->close();
return;
}
//获得用户指定的块表记录
AcDbObjectId blkDefId;
pBlkTab->getAt(blockName, blkDefId);
//创建一个块参照对象
AcDbBlockReference *pBlkRef = new AcDbBlockReference(ptInsert, blkDefId);
//将块参照插入到模型空间中
AcDbBlockTableRecord *pBlkTabRcd;
pBlkTab->getAt(ACDB_MODEL_SPACE, pBlkTabRcd, ZcDb::kForWrite);
pBlkTab->close();
AcDbObjectId objId;
pBlkTabRcd->appendZcDbEntity(objId, pBlkRef);
//判断指定的块表记录是否包含属性
AcDbBlockTableRecord *pBlkDefRcd;
acdbOpenObject(pBlkDefRcd, blkDefId, ZcDb::kForRead);
if (pBlkDefRcd->hasAttributeDefinitions())
{
//创建一个遍历器用来遍历所有的属性
AcDbBlockTableRecordIterator *pItr;
pBlkDefRcd->newIterator(pItr);
AcDbEntity *pEnt;
//遍历块定义属性
for (pItr->start(); !pItr->done(); pItr->step())
{
pItr->getEntity(pEnt, ZcDb::kForWrite);
//检查是否是属性定义
AcDbAttributeDefinition *pAttDef;
pAttDef = AcDbAttributeDefinition::cast(pEnt);
if (pAttDef != NULL)
{
AcDbAttribute *pAtt = new AcDbAttribute();
//从属性定义获取属性对象的对象特征
pAtt->setPropertiesFrom(pAttDef);
//设置属性对象的其他特征
pAtt->setInvisible(pAttDef->isInvisible());//属性是否不可见
//设置属性对象插入点,根据块参照的插入点做相应的位移操作
AcGePoint3d basePt = pAttDef->position();
basePt += pBlkRef->position().asVector();
pAtt->setPosition(basePt);
pAtt->setHeight(pAttDef->height());//设置高
pAtt->setRotation(pAttDef->rotation());//设置旋转角度
//获取属性对象的Tag、 Prompt和TestString
TCHAR *str;
str = pAttDef->tag();
pAtt->setTag(str);
free(str);
pAtt->setFieldLength(pAttDef->fieldLength());
str = pAttDef->textString();
pAtt->setTextString(str);
free(str);
//向块参照中追加属性对象
pBlkRef->appendAttribute(pAtt);
pAtt->close();
}
pEnt->close();
}
delete pItr;
}
//关闭数据库
pBlkRef->close();
pBlkDefRcd->close();
pBlkTabRcd->close();
}