|
楼主 |
发表于 2024-5-4 19:24:28
|
显示全部楼层
- #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);
- };
- #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[128];
- if (acedGetString(NULL, 采用T("\n输入图层名:"), layerName) != RTNORM)
- {
- return;
- }
- Layer::Add(layerName);
- }
- // 设置图层颜色
- void SetLayerColor()
- {
- ACHAR layerName[128];
- // 交互输入字符串:是否允许空格、提示信息、接收字符串的变量
- 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[128];
- 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();
- }
- }
- }
- }
- }
复制代码 |
|