ObjectARX之“九表多词典”操作

CAD二次开发的技术多种多样,无论哪一种技术,其核心永远是对“九表多词典”的增删改查操作,这也是CAD二次开发实用最多的接口。

本文简要介绍基于ObjectARX的“九表多词典”操作,详细资料和接口信息可参考官方文档。

项目搭建

强烈建议使用ObjectARX的项目模板,后期涉及自定义实体等高级开发技术时,该项目模板可以提供很多帮助。

向模型空间添加图元

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
static void LHMyGroupMyCommand() {
AcDbDatabase* pDB = acdbHostApplicationServices()->workingDatabase();
AcDbBlockTable* pBlockTable;
pDB->getSymbolTable(pBlockTable, AcDb::kForWrite);
AcDbBlockTableRecord* pBlockTableRecord;
pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite);
AcDbLine* pLine = new AcDbLine(AcGePoint3d(0, 0, 0), AcGePoint3d(1000, 1000, 0));
AcDbCircle* pCircle = new AcDbCircle(AcGePoint3d(0, 0, 0), AcGeVector3d(0, 0, 1), 500);
AcDbArc* pArc = new AcDbArc(AcGePoint3d(100, 100, 0), 200, 90, 180);
pBlockTableRecord->appendAcDbEntity(pLine);
pBlockTableRecord->appendAcDbEntity(pCircle);
pBlockTableRecord->appendAcDbEntity(pArc);
pLine->close();
pCircle->close();
pArc->close();
pBlockTable->close();
pBlockTableRecord->close();
}

新增块表记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
static void LHMyGroupMyCommand() {
AcDbDatabase* pDB = acdbHostApplicationServices()->workingDatabase();
// 创建新块并插入模型空间
AcDbBlockTableRecord* newBlock = new AcDbBlockTableRecord();
newBlock->setName(L"测试块定义");
newBlock->setOrigin(AcGePoint3d(0, 0, 0));
AcDbLine* pLine2 = new AcDbLine(AcGePoint3d(0, 0, 0), AcGePoint3d(1000, 1000, 0));
AcDbCircle* pCircle2 = new AcDbCircle(AcGePoint3d(0, 0, 0), AcGeVector3d(0, 0, 1), 500);
AcDbArc* pArc2 = new AcDbArc(AcGePoint3d(100, 100, 0), 200, 90, 180);
newBlock->appendAcDbEntity(pLine2);
newBlock->appendAcDbEntity(pCircle2);
newBlock->appendAcDbEntity(pArc2);
AcDbObjectId blockDefinitionId;
pBlockTable->add(blockDefinitionId, newBlock);
newBlock->close();
pLine2->close();
pCircle2->close();
pArc2->close();
// 向模型空间插入块参照
AcDbBlockReference* reference = new AcDbBlockReference(AcGePoint3d(1000, 1000, 0), blockDefinitionId);
pBlockTableRecord->appendAcDbEntity(reference);
reference->close();
pBlockTable->close();
pBlockTableRecord->close();
}

新增图层

1
2
3
4
5
6
7
8
9
10
11
12
13
static void LHMyGroupMyCommand() {
AcDbDatabase* pDB = acdbHostApplicationServices()->workingDatabase();
AcDbLayerTable* pLayerTable;
pDB->getSymbolTable(pLayerTable, AcDb::kForWrite);
AcDbLayerTableRecord* newLayer = new AcDbLayerTableRecord();
newLayer->setName(L"测试图层");
AcCmColor color;
color.setColorIndex(1);
newLayer->setColor(color);
pLayerTable->add(newLayer);
pLayerTable->close();
newLayer->close();
}

词典操作(创建新分组)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
static void LHMyGroupMyCommand() {
AcDbGroup* pGroup = new AcDbGroup(L"新组");
AcDbDictionary* pGroupDict;
acdbHostApplicationServices()->workingDatabase()
->getGroupDictionary(pGroupDict, AcDb::kForWrite);
AcDbObjectId pGroupId;
pGroupDict->setAt(L"新组", pGroup, pGroupId);
pGroupDict->close();
ads_name ss;
acedSSGet(L"A", nullptr, nullptr, nullptr, ss);
Adesk::Int32 len;
acedSSLength(ss, &len);
AcDbObjectId id;
for (Adesk::Int32 i = 0; i < len; i++)
{
ads_name ent;
acedSSName(ss, i, ent);
acdbGetObjectId(id, ent);
pGroup->append(id);
}
pGroup->close();
}

评论