|
Layer.h
#pragma once
class Layer
{
public:
Layer();
~Layer();
// 添加图层:图层名(其实是层表记录的name值)、颜色索引值
static AcDbObjectId Add(const ACHAR *name, int colorIndex = 7);
// 获取图层id:图层名
static AcDbObjectId GetLayerId(const ACHAR *name);
// 设置图层颜色:图层名、颜色索引值
static bool SetColor(const ACHAR *name, int colorIndex);
// 获取 所有 层表记录id 的 列表
static void GetLayerList(AcDbObjectIdArray &layIds);
};
Layer.cpp
#include "stdafx.h"
#include "Layer.h"
Layer::Layer(){}
Layer::~Layer(){}
// 添加图层:图层名(其实是层表记录的name值)、颜色索引值
AcDbObjectId Layer::Add(const ACHAR *name, int colorIndex)
{ // 断言图层名不为空、颜色索引值在范围内
assert(name != NULL);
assert(colorIndex >= 1 && colorIndex <= 255);
AcDbObjectId layerId;
// 获得层表指针
AcDbLayerTable *pLayTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->
getLayerTable(pLayTbl, AcDb::kForWrite);
// 检索层表:通过图层名判断是否有图层(has其实调用了getIdAt)
if (!pLayTbl->has(name))
{ // 新建块表记录:设置层名
AcDbLayerTableRecord *pLayTblRcd = new AcDbLayerTableRecord();
pLayTblRcd->setName(name);
// 新建颜色对象:设置颜色索引值,添加进层表记录
AcCmColor color;
color.setColorIndex(colorIndex);
pLayTblRcd->setColor(color);
// 将层表记录添加进层表中,存储层表记录id,关闭层表记录
pLayTbl->add(pLayTblRcd);
layerId = pLayTblRcd->objectId();
pLayTblRcd->close();
}
// 图层名已存在,打印
else
{
acutPrintf(_T("\n图层%s已存在"), name);
}
pLayTbl->close();
return layerId;
}
// 获取图层id:图层名
AcDbObjectId Layer::GetLayerId(const ACHAR *name)
{ // 断言图层名不为空
assert(name != NULL);
AcDbLayerTable *pLayerTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->
getLayerTable(pLayerTbl, AcDb::kForRead);
AcDbObjectId layerId = AcDbObjectId::kNull;
if (pLayerTbl->has(name))
{ // getAt方法获得图层id并赋值layerId
pLayerTbl->getAt(name, layerId);
}
pLayerTbl->close();
// 若没有则为默认值kNull
return layerId;
}
// 设置图层颜色:图层名、颜色索引值
bool Layer::SetColor(const ACHAR *name, int colorIndex)
{ // 调用本类的方法获得图层id
AcDbObjectId layerId = GetLayerId(name);
bool bRet = false;
AcDbLayerTableRecord *pLayRcd = NULL;
// 获得层表记录指针
if (acdbOpenObject(pLayRcd, layerId, AcDb::kForWrite) == Acad::eOk)
{ // 设置颜色
AcCmColor color;
color.setColorIndex(colorIndex);
// 层表记录修改颜色
pLayRcd->setColor(color);
bRet = true;
pLayRcd->close();
}
return bRet;
}
// 获取 所有 层表记录id 的 列表
void Layer::GetLayerList(AcDbObjectIdArray &layIds)
{ // 获得层表对象指针、获得层表遍历器指针
AcDbLayerTable *pLayTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->
getLayerTable(pLayTbl, AcDb::kForRead);
AcDbLayerTableIterator *pItr = NULL;
pLayTbl->newIterator(pItr);
AcDbLayerTableRecord *pLayerTblRcd = NULL;
// 层表遍历器遍历获取每个层表记录对象指针
for (pItr->start();!pItr->done();pItr->step())
{
if (pItr->getRecord(pLayerTblRcd, AcDb::kForRead) == Acad::eOk)
{ // 将每个层表记录id累加到层表记录id列表中
layIds.append(pLayerTblRcd->objectId());
pLayerTblRcd->close();
}
}
delete pItr;
pLayTbl->close();
} |
|