-
Notifications
You must be signed in to change notification settings - Fork 41
/
CDwgDatebaseUtil.cpp
104 lines (91 loc) · 2.55 KB
/
CDwgDatebaseUtil.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "stdafx.h"
#include "CDwgDatebaseUtil.h"
CDwgDatebaseUtil::CDwgDatebaseUtil()
{
}
CDwgDatebaseUtil::~CDwgDatebaseUtil()
{
}
AcDbObjectId CDwgDatebaseUtil::PostToModelSpace(AcDbEntity * pEntity)
{
Acad::ErrorStatus es;
//检查输入参数有效性
assert(pEntity != NULL);
//获取当前图形数据库的块表
AcDbBlockTable *pBlockTable;
es=acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable, AcDb::kForRead);
//获取模型空间对应的块表记录
AcDbBlockTableRecord *pBlockTableRecord;
es=pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite);
//将实体添加到空间对应的块表记录中
AcDbObjectId ObjectID;
es=pBlockTableRecord->appendAcDbEntity(ObjectID, pEntity);
if (es!=Acad::eOk)
{
pBlockTable->close();
pBlockTableRecord->close();
//添加失败时,要delete,成功并且appendAcdbEntity之后,由数据库来操作该对象
delete pEntity;
pEntity = NULL;
return AcDbObjectId::kNull;
}
//关闭模型空间和实体
pBlockTable->close();
pBlockTableRecord->close();
pEntity->close();
return ObjectID;
}
AcDbObjectIdArray CDwgDatebaseUtil::GetAllEntityIDs(const TCHAR * layerName)
{
AcDbObjectIdArray EntityIDs;
bool bifFilterlayer = false;
AcDbObjectId layerid;
if (layerName != NULL)
{
AcDbLayerTable *layertable=NULL;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(layertable, AcDb::kForRead);
if (!layertable->has(layerName))
{
layertable->close();
acutPrintf(_T("在当前模型空间未找到名称为: %s 的图层"), layerName);
return EntityIDs;
}
layertable->getAt(layerName, layerid);
layertable->close();
bifFilterlayer = true;
}
AcDbBlockTable *pBlockTable;
acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable, AcDb::kForRead);
AcDbBlockTableRecord *PBlockTableRecoard;
pBlockTable->getAt(ACDB_MODEL_SPACE, PBlockTableRecoard, AcDb::kForRead);
pBlockTable->close();
AcDbBlockTableRecordIterator *it=NULL;
PBlockTableRecoard->newIterator(it);
for (it->start();!it->done();it->step())
{
AcDbEntity *pEntity = NULL;
Acad::ErrorStatus es = it->getEntity(pEntity, AcDb::kForRead);
if (Acad::eOk==es)
{
if (bifFilterlayer) //如果图层名不为空,过滤出符合图层名称的实体
{
if (pEntity->layerId() == layerid)
{
EntityIDs.append(pEntity->objectId());
}
}
else //否则把所有的都加入id集
{
EntityIDs.append(pEntity->objectId());
}
pEntity->close();
}
else
{
acutPrintf(TEXT("\nCDwgDatebaseUtil::GetAllEntityIDs中打开实体失败,(错误代码:%d)"), (int)es);
}
}
delete it;
PBlockTableRecoard->close();
return EntityIDs;
}