ObjectARX_AcDbAttributeDefinition属性定义对象

作者: admin 分类: C++,CAD,ObjectARX 发布时间: 2020-08-05 20:25

转自:https://my.oschina.net/u/2930533/blog/1592792

1)先介绍俩个cad的命令:

命令BATTMAN : 块属性管理器(批量修改图块属性)

093726_Kz3S_2930533.png

命令BEDIT:编辑块定义

094518_FAbC_2930533.png

(2)创建带属性的块

    // 根据用户的输入设置块表记录的名称

TCHAR blkName[40];

if (acedGetString(Adesk::kFalse, _T("\n输入图块的名称:"), blkName) !=RTNORM)

return;

// 获得当前图形数据库的块表

AcDbBlockTable *pBlkTbl = NULL;

acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlkTbl, AcDb::kForWrite);

if (NULL == pBlkTbl)

return;

// 创建新的块表记录

AcDbBlockTableRecord *pBlkTblRcd = new AcDbBlockTableRecord();

pBlkTblRcd->setName(blkName);

// 将块表记录添加到块表中

AcDbObjectId blkDefId;

pBlkTbl->add(blkDefId, pBlkTblRcd);

pBlkTbl->close();

// 向块表记录中添加实体

AcGePoint3d ptStart(-10, 0, 0), ptEnd(10, 0, 0);

AcDbLine *pLine1 = new AcDbLine(ptStart, ptEnd); // 创建一条直线

ptStart.set(0, -10, 0);

ptEnd.set(0, 10, 0);

AcDbLine *pLine2 = new AcDbLine(ptStart, ptEnd); // 创建一条直线

AcGeVector3d vecNormal(0, 0, 1);

AcDbCircle *pCircle = new AcDbCircle(AcGePoint3d::kOrigin,vecNormal, 6);//创建一个圆

// 创建一个属性块 

AcDbAttributeDefinition *pAttDef = new AcDbAttributeDefinition(ptEnd, _T("默认值"),_T( "标记"), _T("提示")); 

AcDbObjectId entId;

pBlkTblRcd->appendAcDbEntity(entId, pLine1);

pBlkTblRcd->appendAcDbEntity(entId, pLine2);

pBlkTblRcd->appendAcDbEntity(entId, pCircle);

pBlkTblRcd->appendAcDbEntity(entId, pAttDef);

// 关闭实体和块表记录

pLine1->close();

pLine2->close();

pCircle->close();

pAttDef->close();

pBlkTblRcd->close(); 

(3)插入带属性图块:可以直接使用cad提供的INSERT命令

      // 获得用户输入的块定义名称 

TCHAR blkName[40];

if (acedGetString(Adesk::kFalse, _T("\n输入图块的名称:"), blkName) !=RTNORM)

return;

// 获得当前数据库的块表

AcDbBlockTable *pBlkTbl = NULL;

acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlkTbl, AcDb::kForWrite);

if (NULL == pBlkTbl)

return;

// 查找用户指定的块定义是否存在

CString strBlkDef;

strBlkDef.Format(_T("%s"), blkName);

if (!pBlkTbl->has(strBlkDef))

{

acutPrintf(_T("\n当前图形中未包含指定名称的块定义!"));

pBlkTbl->close();

return;

}

// 获得用户输入的块参照的插入点

ads_point pt;

if (acedGetPoint(NULL, _T("\n输入块参照的插入点:"), pt) != RTNORM)

pBlkTbl->close();

return;

}

AcGePoint3d ptInsert = asPnt3d(pt);

// 获得用户指定的块表记录

AcDbObjectId blkDefId;

pBlkTbl->getAt(strBlkDef, blkDefId);

// 将块参照添加到模型空间

AcDbBlockTableRecord *pBlkTblRcd = NULL;

pBlkTbl->getAt(ACDB_MODEL_SPACE, pBlkTblRcd,AcDb::kForWrite);

pBlkTbl->close();

if (NULL == pBlkTblRcd)

return;

// 创建块参照对象

AcDbBlockReference *pBlkRef = new AcDbBlockReference(ptInsert, blkDefId);

AcDbObjectId entId;

pBlkTblRcd->appendAcDbEntity(entId, pBlkRef); 

// 判断指定的块表记录是否包含属性定义

AcDbBlockTableRecord *pBlkDefRcd = NULL;

acdbOpenObject(pBlkDefRcd, blkDefId, AcDb::kForRead);

if (NULL == pBlkDefRcd)

{

pBlkRef->close();

pBlkTblRcd->close();

return;

}

if (pBlkDefRcd->hasAttributeDefinitions())

{

AcDbBlockTableRecordIterator *pItr = NULL;

pBlkDefRcd->newIterator(pItr);

AcDbEntity *pEnt = NULL;

AcDbAttributeDefinition *pAttDef = NULL;

for (pItr->start(); !pItr->done(); pItr->step())

{

pItr->getEntity(pEnt, AcDb::kForRead);

// 检查实体是否是属性定义

pAttDef = AcDbAttributeDefinition::cast(pEnt);

if (pAttDef != NULL)

{

// 创建一个新的属性对象

AcDbAttribute *pAtt = new AcDbAttribute();

// 从属性定义获得属性对象的对象特性

pAtt->setPropertiesFrom(pAttDef);

// 设置属性对象的其他特性

pAtt->setInvisible(pAttDef->isInvisible());

AcGePoint3d ptBase = pAttDef->position();

ptBase += pBlkRef->position().asVector();

pAtt->setPosition(ptBase);

pAtt->setHeight(pAttDef->height());

pAtt->setRotation(pAttDef->rotation());

// 获得属性对象的Tag、Prompt和TextString

ACHAR *pStr;

pStr = pAttDef->tag();

pAtt->setTag(pStr);

free(pStr);

pStr = pAttDef->prompt();

acutPrintf(_T("\n%s%s"), pStr);

free(pStr);

pAtt->setFieldLength(30);

pAtt->setTextString(_T("当前值"));

// 向块参照追加属性对象

pBlkRef->appendAttribute(pAtt);

pAtt->close();

}

pEnt->close();

}

pAttDef->close();

delete pItr;

}

// 关闭数据库的对象

pBlkRef->close();

pBlkTblRcd->close();

pBlkDefRcd->close();

(4)获取参照块的属性

    ads_name ssResult;

ads_point ssptres;

acedEntSel(_T("选择一个块:\n"), ssResult, ssptres);

AcDbObjectId blkRefId;

acdbGetObjectId(blkRefId, ssResult);

AcDbBlockReference *pRef = NULL;

Acad::ErrorStatus es = acdbOpenObject(pRef, blkRefId, AcDb::kForWrite);  

if (NULL == pRef || es != eOk)

return ;

AcDbObjectId  blkrecId = pRef->blockTableRecord();

pRef->close();

AcDbBlockTableRecord *pBlkDefRcd = NULL;

es=acdbOpenObject(pBlkDefRcd, blkrecId , AcDb::kForRead); 

if (NULL == pBlkDefRcd || es != eOk)

return ;

if (pBlkDefRcd->hasAttributeDefinitions())  //是否有属性

{

//当前值

AcDbObjectIterator* pItr = pRef->attributeIterator();

for(pItr->start(); !pItr->done(); pItr->step())

{

AcDbObjectId attid = pItr->objectId();

AcDbAttribute* pAttr = NULL;

pRef->openAttribute(pAttr, attid, AcDb::kForRead);

ACHAR* text = pAttr->textString();

acutPrintf(_T("当前值:%s\n"), text);

pAttr->close();

free(text);

}

delete pItr;

//属性

AcDbBlockTableRecordIterator *pTItr = NULL;

pBlkDefRcd->newIterator(pTItr);

AcDbEntity* pEnt = NULL;

for(pTItr->start(); !pTItr->done(); pTItr->step())

{

pTItr->getEntity(pEnt, AcDb::kForRead);

AcDbAttributeDefinition* pAttDef = AcDbAttributeDefinition::cast(pEnt);

if(pAttDef != NULL)

{

ACHAR * strTemp;

strTemp = pAttDef->tag();

acutPrintf(_T("标记:%s\n"), strTemp);

free(strTemp);

strTemp = pAttDef->prompt();

acutPrintf(_T("提示:%s\n"), strTemp);

free(strTemp);

strTemp=pAttDef->textString();

acutPrintf(_T("值:%s\n"), strTemp);

free(strTemp);

pAttDef->close();

}

pEnt->close();

}

delete pTItr;

}

pBlkDefRcd->close();

 

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

发表评论

标签云