{
AcDbDatabase* pDatabase = acdbHostApplicationServices()->workingDatabase();
AcDbLayerTable* pLayerTable;
pDatabase->getLayerTable(pLayerTable, AcDb::kForRead);
AcDbLayerTableRecord* pLayerRecord;
if (pLayerTable->getAt(layerName, pLayerRecord, AcDb::kForRead) == Acad::eOk)
{
// 获取图层的 Object ID
AcDbObjectId layerId = pLayerRecord->objectId();
pLayerRecord->close();
AcDbBlockTable* pBlockTable;
pDatabase->getBlockTable(pBlockTable, AcDb::kForRead);
AcDbBlockTableRecord* pBlockTableRecord;
if (pBlockTable->getAt(ACDB采用MODEL采用SPACE, pBlockTableRecord, AcDb::kForWrite) == Acad::eOk)
{
// 遍历模型空间中的所有实体
AcDbBlockTableRecordIterator* pIterator;
pBlockTableRecord->newIterator(pIterator);
for (; !pIterator->done(); pIterator->step())
{
AcDbEntity* pEntity;
if (pIterator->getEntity(pEntity, AcDb::kForWrite) == Acad::eOk)
{
// 检查实体所在的图层是否为指定图层
if (pEntity->layerId() == layerId)
{
// 删除实体
pEntity->erase();
}
pEntity->close();
}
}
delete pIterator;
pBlockTableRecord->close();
}
pBlockTable->close();
}
pLayerTable->close();
}
// 新建一个图层
void CLayerOperator::NewLayer(ACHAR* layerName)
{
//获得当前图形的层表
AcDbLayerTable *pLayerTbl;
acdbHostApplicationServices()
->workingDatabase()
->getLayerTable(pLayerTbl,AcDb::kForWrite);
//是否已经包含指定的层表记录
if(pLayerTbl->has(layerName)){
pLayerTbl->close();
acedPrompt(采用T("对应名称的图层已经存在!"));
return;
}
//创建层表记录
AcDbLayerTableRecord *pLayerTblRcd;
pLayerTblRcd=new AcDbLayerTableRecord();
pLayerTblRcd->setName(layerName); //这里的layerName需要用(采用T("?"))这种格式来命名
//将新建的层表记录添加到层表中
AcDbObjectId layerTblRcdId;
pLayerTbl->add(layerTblRcdId,pLayerTblRcd);
//设置图形的当前图层
acdbHostApplicationServices()
->workingDatabase()
->setClayer(layerTblRcdId);
//关闭层表
pLayerTblRcd->close();
pLayerTbl->close();
}
// 使用图形对话框设置指定图层的颜色
void CLayerOperator::SetLayerColor(ACHAR* layerName)
{
//获得当前图形的层表
AcDbLayerTable *pLayerTbl;
acdbHostApplicationServices()
->workingDatabase()
->getLayerTable(pLayerTbl,AcDb::kForRead);
//判断是否包含指定名称的层表记录
if(!pLayerTbl->has(layerName)){
pLayerTbl->close();
return;
}
//获得指定层表记录的指针
AcDbLayerTableRecord *pLayerTblRcd;
pLayerTbl->getAt(layerName,pLayerTblRcd,AcDb::kForWrite);
//弹出“颜色”对话框
AcCmColor oldColor=pLayerTblRcd->color();
//图层修改前的颜色
int nCurColor=oldColor.colorIndex();
//用户选择的颜色
int nNewColor=oldColor.colorIndex();
if(acedSetColorDialog(nNewColor,Adesk::kFalse,nCurColor)){
AcCmColor color;
color.setColorIndex(nNewColor);
pLayerTblRcd->setColor(color);
}
//关闭层表
pLayerTblRcd->close();
pLayerTbl->close();
}
// 删除一个图层
void CLayerOperator::DelLayer(ACHAR* layerName)
{
//获得当前图形的层表
AcDbLayerTable *pLayerTbl;
acdbHostApplicationServices()
->workingDatabase()
->getLayerTable(pLayerTbl,AcDb::kForRead);
//判断是否包含指定名称的层表记录
if(!pLayerTbl->has(layerName)){
pLayerTbl->close();
return;
}
//获得指定层表记录的指针
AcDbLayerTableRecord *pLayerTblRcd;
pLayerTbl->getAt(layerName,pLayerTblRcd,AcDb::kForWrite);
//为其设置“删除”标记
pLayerTblRcd->erase();
pLayerTblRcd->close(); //关闭层表
pLayerTbl->close();
}
//获取指定图层上所有实体ID
2 AcDbObjectIdArray GetAllEntityId(const TCHAR* layername)
3 {
4 AcDbObjectIdArray entIds;
5 bool bFilterlayer = false;
6 AcDbObjectId layerId;
7 //获取指定图层对象ID
8 if (layername != NULL)
9 {
10 AcDbLayerTable *pLayerTbl = NULL;
11 acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLayerTbl, AcDb::kForRead);
12 if (!pLayerTbl->has(layername))
13 {
14 pLayerTbl->close();
15 return entIds;
16 }
17 pLayerTbl->getAt(layername, layerId);
18 pLayerTbl->close();
19 bFilterlayer = true;
20 }
21 //获得块表
22 AcDbBlockTable *pBlkTbl = NULL;
23 acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBlkTbl, AcDb::kForRead);
24 //块表记录
25 AcDbBlockTableRecord *pBlkTblRcd = NULL;
26 pBlkTbl->getAt(ACDB采用MODEL采用SPACE, pBlkTblRcd, AcDb::kForRead);
27 pBlkTbl->close();
28 //创建遍历器,依次访问模型空间中的每一个实体
29 AcDbBlockTableRecordIterator *it = NULL;
30 pBlkTblRcd->newIterator(it);
31 for (it->start(); !it->done(); it->step())
32 {
33 AcDbEntity *pEnt = NULL;
34 Acad::ErrorStatus es = it->getEntity(pEnt, AcDb::kForRead);
35 if (es == Acad::eOk)
36 {
37 if (bFilterlayer)//过滤图层
38 {
39 if (pEnt->layerId() == layerId)
40 {
41 entIds.append(pEnt->objectId());
42 }
43 }
44 else
45 {
46 // entIds.append(pEnt->objectId());
47 pEnt->close();
48 }
49 }
50 }
51 delete it;
52 pBlkTblRcd->close();
53 return entIds;
54 }
#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 boolSetColor(const ACHAR *name, int colorIndex);
// 获取 所有 层表记录id 的 列表
static void GetLayerList(AcDbObjectIdArray &layIds);
};
#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();
}
#include "StdAfx.h"
void AddCommands();
void CreateLayer();
void SetLayerColor();
void DeleteLayer();
void ExportLayer();
void ImPortLayer();
#include "StdAfx.h"
#include "Commands.h"
#include "Editor.h"
#include "Layer.h"
#include <vector>
#include "ConvertUtil.h"
#include "TextFileUtil.h"
#include "StringUtil.h"
void AddCommands()
{ // 新建图层
Editor::AddCommand(L"Add", ACRX采用CMD采用MODAL, CreateLayer);
// 修改图层颜色
Editor::AddCommand(L"setcolor", ACRX采用CMD采用MODAL, SetLayerColor);
// 删除图层
Editor::AddCommand(L"delete", ACRX采用CMD采用MODAL, DeleteLayer);
// 导出图层到文件
Editor::AddCommand(L"exportlayer", ACRX采用CMD采用MODAL, ExportLayer);
// 导入图层到文件
Editor::AddCommand(L"importlayer", ACRX采用CMD采用MODAL, ImPortLayer);
}
// 添加图层
void CreateLayer()
{
ACHAR layerName;
if (acedGetString(NULL, 采用T("\n输入图层名:"), layerName) != RTNORM)
{
return;
}
Layer::Add(layerName);
}
// 设置图层颜色
void SetLayerColor()
{
ACHAR layerName;
// 交互输入字符串:是否允许空格、提示信息、接收字符串的变量
if (acedGetString(NULL, 采用T("\n输入图层名:"), layerName) != RTNORM)
{
return;
}
// 获得层名对应的id
AcDbObjectId layerId = Layer::GetLayerId(layerName);
if (layerId != AcDbObjectId::kNull)
{ // 调出颜色选项板:接收选择的颜色索引、是否开启随层随块选项、默认颜色索引
int newColor;
if (acedSetColorDialog(newColor, Adesk::kFalse, 1))
{
Layer::SetColor(layerName, newColor);
}
}
else
{
acutPrintf(采用T("\n查无此图层!"));
}
}
// 删除图层
void DeleteLayer()
{
ACHAR layerName;
if (acedGetString(NULL, 采用T("\n请输入待删除的图层名:"), layerName) != RTNORM)
return;
AcDbObjectId layerId = Layer::GetLayerId(layerName);
if (layerId != AcDbObjectId::kNull)
{
AcDbLayerTableRecord *pRcd = NULL;
if (acdbOpenObject(pRcd, layerId, AcDb::kForWrite) == Acad::eOk)
{ // 删除操作,只是在层表记录上打个标记,关闭文件时永久写入
pRcd->erase();
pRcd->close();
}
}
else
acutPrintf(采用T("\n查无此图层!"));
}
// 导出层表文件到桌面
void ExportLayer()
{ // 获取层表所有层表记录id
AcDbObjectIdArray layIds;
Layer::GetLayerList(layIds);
// TextFileUtil::ToFile函数的第二个参数
std::vector<CString>layLines;
// 获得并操作每个层表记录对象指针:通过传入的layIds
for (int i = 0; i < layIds.length(); i++)
{ // 获取当前循环的层表记录
AcDbLayerTableRecord *pLayRcd = NULL;
if (acdbOpenObject(pLayRcd, layIds.at(i), AcDb::kForRead) == Acad::eOk)
{ // 当前层表信息容器:获取当前层表记录的所有信息
std::vector<CString>layInfos;
// 层表记录名称
ACHAR *szLayerName;
pLayRcd->getName(szLayerName);
layInfos.push采用back(szLayerName);
acutDelString(szLayerName); // 释放指针
// 层表记录颜色
AcCmColor col = pLayRcd->color();
layInfos.push采用back(ConvertUtil::ToString(col.colorIndex()));
// 层表记录->线型id->名称
AcDbLinetypeTableRecord *pLineTypeRcd = NULL;
acdbOpenObject(pLineTypeRcd, pLayRcd->linetypeObjectId(), AcDb::kForRead);
ACHAR *szLinetypeName;
pLineTypeRcd->getName(szLinetypeName);
pLineTypeRcd->close();
layInfos.push采用back(szLinetypeName);
acutDelString(szLinetypeName); // 释放指针
// 层表记录线宽
AcDb::LineWeight lineWeight = pLayRcd->lineWeight();
int nVal = (int)lineWeight;
layInfos.push采用back(ConvertUtil::ToString(nVal));
// 当前层表信息容器 拼接成 字符串,加到layIds
CString strLayer = StringUtil::Join(layInfos, 采用T(","));
layLines.push采用back(strLayer);
pLayRcd->close();
}
}
// 新建桌面文件并接收layLines
TextFileUtil::ToFile(采用T("C:\\Users\\Administrator\\Desktop\\test.txt"), layLines);
}
void ImPortLayer()
{
std::vector<CString>lines;
CString fileName = 采用T("C:\\Users\\Administrator\\Desktop\\test.txt");
// cstring类有隐式类型转换方法:将cstring转为const achar*类型
if (TextFileUtil::FromFile(fileName, lines))
{ // 提取容器每个元素,并按 分隔符 切割字符串
for (int i = 0; i < lines.size(); i++)
{
std::vector<CString>layInfo;
StringUtil::Split(lines.at(i), 采用T(","), layInfo);
if (layInfo.size() == 4)
{ // 检索层名:如果层表中没记录新增,有记录,不新增直接修改
CString layName = layInfo.at(0);
AcDbObjectId layId = Layer::GetLayerId(layName);
if (layId.isNull())
{
layId = Layer::Add(layName);
}
AcDbLayerTableRecord *pLayRcd = NULL;
if (acdbOpenObject(pLayRcd, layId, AcDb::kForWrite) == Acad::eOk)
{ // 修改颜色
AcCmColor col;
Adesk::UInt16 colorIndex = 采用ttoi(layInfo.at(1));
col.setColorIndex(colorIndex);
pLayRcd->setColor(col);
// 修改线型
AcDbLinetypeTable *pLinetypeTbl = NULL;
AcDbObjectId linetypeId;
// 打开线型表
acdbHostApplicationServices()->workingDatabase()->
getLinetypeTable(pLinetypeTbl, AcDb::kForRead);
// 检索线性表:如果有同名记录
if (pLinetypeTbl->has(layInfo.at(2)))
{ // 获得线型记录id
pLinetypeTbl->getAt(layInfo.at(2), linetypeId);
// 层表记录设置线型id
pLayRcd->setLinetypeObjectId(linetypeId);
}
pLinetypeTbl->close();
// 修改线宽(采用ttoi能将CString转换成整形)
AcDb::LineWeight lineWeight =
(AcDb::LineWeight) 采用ttoi(layInfo.at(3));
pLayRcd->setLineWeight(lineWeight);
pLayRcd->close();
}
}
}
}
}
ObjectARX选择图层的上所有实体
Acad::ErrorStatus selectEntityInLayer(std::wstring nLayerName, AcDbObjectIdArray&nIDs)
{
Acad::ErrorStatus es = Acad::eOk;
ads采用name ents;
structresbuf* rb;
rb = acutNewRb(AcDb::kDxfLayerName);
rb->restype = 8;
acutNewString(nLayerName.c采用str(), rb->resval.rstring);
rb->rbnext = NULL;
acedSSGet(L"X", NULL, NULL, rb, ents);
longentNums = 0;
acedSSLength(ents, &entNums);
if (entNums == 0)
es = Acad::eInvalidInput;
else
{
for (long a = 0; a < entNums; a++)
{
AcDbObjectIdobjId;
ads采用name ent;
acedSSName(ents, a, ent);
acdbGetObjectId(objId, ent);
nIDs.append(objId);
}
}
acedSSFree(ents);
acutRelRb(rb);
returnes;
} ObjectARX如何锁定一个图层
一般来说,更改图层设置后需要重生成图纸,但是重生成时间开销太大。为了降低时间开销,可以采取以下两种方法。
一、通过设置LAYLOCKFADECTL避免重生成
找到需要锁定的图层,打开它,设置其为锁定状态。但是注意,使用该方式前必须设置LAYLOCKFADECTL系统变量为一个新值,设定图层锁定后再设回原值。
void SetLayLock()
{
int iOldLockFade, iNewLockFade;
struct resbuf buf;
acedGetVar(采用T("LAYLOCKFADECTL"), &buf);
iOldLockFade = buf.resval.rint;
iNewLockFade = iOldLockFade ? -iOldLockFade : 1;
buf.resval.rint = iNewLockFade;
acedSetVar(采用T("LAYLOCKFADECTL"), &buf); //这一步必不可少,否则不会暗显锁定图层的对象
//提示用户选择一个实体
ads采用name ss;
ads采用point pt;
if (RTNORM != acedEntSel(采用T("请选择一个实体"), ss, pt))
return;
AcDbObjectId id;
if (Acad::eOk == acdbGetObjectId(id, ss))
{
AcDbEntity* pEnt;
if (Acad::eOk == acdbOpenObject(pEnt, id, AcDb::kForRead))
{
id = pEnt->layerId();
pEnt->close();
AcDbLayerTableRecord* pLyrRec;
if (Acad::eOk == acdbOpenObject(pLyrRec, id, AcDb::kForWrite))
{
pLyrRec->setIsLocked(true);
pLyrRec->close();
}
}
}
buf.resval.rint = iOldLockFade;
acedSetVar(采用T("LAYLOCKFADECTL"), &buf);//这一步必不可少
}
这种方法同样不需要重生成图纸,但是并不推荐,首先是CAD命令行最多只能输入256个字符,当需要锁定多个图层(在ads采用command的实参字符串中,每个图层名中用,隔开)时,ads采用command实参字符串长度可能超过256;其次,图层名称中含有诸如#、.等通配符时需要特殊处理;最后,由于未知的原因,采用发送命令的方式无法锁定部分图层。
void SetLayLock2()
{
//提示用户选择一个实体
ads采用name ss;
ads采用point pt;
if (RTNORM != acedEntSel(采用T("请选择一个实体"), ss, pt))
return;
CString sLayer;
TCHAR* pszLayer;
AcDbObjectId id;
if (Acad::eOk == acdbGetObjectId(id, ss))
{
AcDbEntity* pEnt;
if (Acad::eOk == acdbOpenObject(pEnt, id, AcDb::kForRead))
{
pszLayer= pEnt->layer();
sLayer = pszLayer;
acutDelString(pszLayer);
pEnt->close();
}
}
if (!sLayer.IsEmpty())
ads采用command(RTSTR, 采用T("-layer"), RTSTR, 采用T("lo"), RTSTR, sLayer, RTSTR, 采用T(""), RTNONE);
}
OBJECT ARX 操作图层
//添加图层
static void TESTaddlayercmd(){
CString strLayerName;
if(acedGetString(Adesk::kFalse,采用T("\n输入层名称"),strLayerName.GetBuffer()) != RTNORM){
return;
}
获得当前图形的层表
AcDbLayerTable* pLayerTbl;
acdbHostApplicationServices()->workingDatabase()->getLayerTable(pLayerTbl,AcDb::kForWrite);
是否已经包含制定的层表记录
if(pLayerTbl->has(strLayerName)){
pLayerTbl->close();
return;
}
创建新的层表记录
AcDbLayerTableRecord* pLayerTblRcd;
pLayerTblRcd = new AcDbLayerTableRecord();
pLayerTblRcd->setName(strLayerName);
将新创建的层表记录添加到层表中
AcDbObjectId layerTblRcdId;
pLayerTbl->add(layerTblRcdId,pLayerTblRcd);
acdbHostApplicationServices()->workingDatabase()->setClayer(layerTblRcdId);
pLayerTblRcd->close();
pLayerTbl->close();
}
//修改图层颜色============================
static void TESTlayercolorcmd(){
CString strLayerName;
if(acedGetString(Adesk::kFalse,采用T("\n输入图层的名称:"),strLayerName.GetBuffer()) != RTNORM){
return;
}
获得当前的图层列表
AcDbLayerTable* pLayerTbl;
acdbHostApplicationServices()->workingDatabase()->getLayerTable(pLayerTbl,AcDb::kForRead);
判断是否包含指定名称的层表记录
if(!pLayerTbl->has(strLayerName)){
pLayerTbl->close();
return;
}
获得制定层表记录的指针
AcDbLayerTableRecord* pLayerTblRcd;
pLayerTbl->getAt(strLayerName,pLayerTblRcd,AcDb::kForWrite);
弹出颜色对话框
AcCmColor oldColor = pLayerTblRcd->color();
int nCurColor = oldColor.colorIndex();//旧的颜色
int nNewColor = oldColor.colorIndex();//用户选择的颜色
if(acedSetColorDialog(nNewColor,Adesk::kFalse,nCurColor)){
AcCmColor color;
color.setColorIndex(nNewColor);
pLayerTblRcd->setColor(color);
}
pLayerTblRcd->close();
pLayerTbl->close();
}
//删除图层
static void TESTdellayercmd(){
CString strLayerName;
if(acedGetString(Adesk::kFalse,采用T("\n输入图层名称"),strLayerName.GetBuffer()) != RTNORM){
return;
}
获得当前的图层列表
AcDbLayerTable* pLayerTbl;
acdbHostApplicationServices()->workingDatabase()->getLayerTable(pLayerTbl,AcDb::kForRead);
判断是否包含指定名称的层表记录
if(!pLayerTbl->has(strLayerName)){
pLayerTbl->close();
return;
}
获得制定层表记录的指针
AcDbLayerTableRecord* pLayerTblRcd;
pLayerTbl->getAt(strLayerName,pLayerTblRcd,AcDb::kForWrite);
pLayerTblRcd->erase();
pLayerTblRcd->close();
pLayerTbl->close();
}
导出层的信息到文本文件中
static void TESTexportlayercmd(){
//创建要导出的文本文件
CStdioFile f;
CFileException e;
CString pFileName;
pFileName = 采用T("D:\\layer.txt");
if(!f.Open(pFileName.GetString(),CFile::modeCreate|CFile::modeWrite,&e)){
acutPrintf(采用T("\n创建文件失败"));
return;
}
获得层表指针
AcDbLayerTable *pLayerTbl;
AcDbLayerTableRecord* pLayerTblRcd;
acdbHostApplicationServices()->workingDatabase()->getLayerTable(pLayerTbl,AcDb::kForRead);
使用遍历器访问每条层表记录
AcDbLayerTableIterator* pItr;
pLayerTbl->newIterator(pItr);
for(pItr->start();!pItr->done();pItr->step()){
pItr->getRecord(pLayerTblRcd,AcDb::kForRead);
输出图层的信息
CString strLayerInfo;
TCHAR* layerName;
pLayerTblRcd->getName(layerName);
名称
strLayerInfo = layerName;
free(layerName);
strLayerInfo.Append(采用T(","));
颜
CString strColor;
AcCmColor color = pLayerTblRcd->color();
strColor.Format(采用T("%d"),color.colorIndex());
strLayerInfo.Append(strColor);
线型
CString strLineType;
AcDbLinetypeTableRecord* pLinetypeTblRcd;
acdbOpenObject(pLinetypeTblRcd,pLayerTblRcd->linetypeObjectId(),AcDb::kForRead);
TCHAR* linetypeName;
pLinetypeTblRcd->getName(linetypeName);
pLinetypeTblRcd->close();
strLineType = linetypeName;
free(linetypeName);
strLayerInfo.Append(strLineType);
strLayerInfo.Append(采用T(","));
线宽
CString strLineWeight;
AcDb::LineWeight lineWeight = pLayerTblRcd->lineWeight();
strLineWeight.Format(采用T("%d"),lineWeight);
strLayerInfo.Append(strLineWeight);
/写文件
f.WriteString(strLayerInfo);
f.WriteString(采用T("\n"));
pLayerTblRcd->close();
}
delete pItr;
pLayerTbl->close();
}
-修改实体的图层 CH3采用2
static void aaaMyGroupMyCommand () {
ads采用name srcSS;
ads采用name targEnt;
int rc;
ads采用point pickPt;
acedPrompt(采用T("\nSelect entities for layer change!"));
rc = acedSSGet(NULL, NULL, NULL, NULL, srcSS);
if (rc != RTNORM)
{
acutPrintf(采用T("\nNo entities selected!"));
return;
}
rc = acedEntSel(采用T("Select target layer entity."),targEnt,pickPt);
switch (rc)
{
case RTERROR:
acutPrintf(采用T("\nNothing selected!"));
break;
case RTCAN:
acutPrintf(采用T("\nUser canceled."));
case RTNORM:
COtherUtil::chgEntsLyr(targEnt, srcSS);
break;
}
acedSSFree(srcSS);
}
void COtherUtil::chgEntsLyr(ads采用name ent, ads采用name ss)
{
acutPrintf(采用T("\n进入chgEntsLyr()函数"));
long idx;
long lenSS;
ACHAR lyrName;
ads采用name ssEntName;
struct resbuf *rbTargEnt;
struct resbuf *rbSSEnt;
struct resbuf *rbTrav;
int rc;
acutPrintf(采用T("\n获取目标实体的图层名."));
rbTargEnt = acdbEntGet(ent);
if (!rbTargEnt)
{
acutPrintf(采用T("\nFailed to get target entities data."));
return;
}
rbTrav = rbTargEnt;
while (rbTrav)
{
switch (rbTrav->restype)
{
case 8:
strcpy(reinterpret采用cast<char *>(lyrName),
reinterpret采用cast<const char*>(rbTrav->resval.rstring));
break;
}
rbTrav=rbTrav->rbnext;
}
acutPrintf(采用T("\n释放rbTargEnt."));
if (rbTargEnt)
{
acutRelRb(rbTargEnt);
}
acutPrintf(采用T("\n遍历选择集."));
rc = acedSSLength(ss, reinterpret采用cast<Adesk::Int32*>(&lenSS));
if (rc != RTNORM)
{
acutPrintf(采用T("\nInvalid or empty selection set."));
return;
}
acutPrintf(采用T("\n进入for循环."));
for (idx = 0; idx < lenSS; idx++)
{
rc = acedSSName(ss, idx, ssEntName);
if (rc != RTNORM)
{
break;
}
//Get ssEntName entity data
rbSSEnt = acdbEntGet(ssEntName);
if (!rbSSEnt)
{
break;
}
rbTrav = rbSSEnt;
acutPrintf(采用T("\n进入while循环."));
while (rbTrav)
{
switch (rbTrav->restype)
{
case 8:
strcpy(reinterpret采用cast<char *>(rbTrav->resval.rstring),
reinterpret采用cast<const char*>(lyrName));
break;
}
rbTrav = rbTrav->rbnext;
}
acutPrintf(采用T("\nacdbEntMod()函数."));
rc = acdbEntMod(rbSSEnt);
if (rc != RTNORM)
{
acutPrintf(采用T("\nFailded to modify the entity."));
}
//Release the resbuf
if (rbSSEnt)
{
acutRelRb(rbSSEnt);
}
}
}
页:
1
[2]