一、层表
- 不可删除:图层为当前图层时,不可删除
- 不可知:图层上有实体,删除图层后,实体无依附图层,产生不可知后果
1.1 层表类
- 模板:以第(三)篇文件结构为基础,构建自己的类库
- 在创建SymbolTable筛选器中添加
- 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(); }
1.2 测试代码
- Commands.h
#include "StdAfx.h" void AddCommands(); void CreateLayer(); void SetLayerColor(); void DeleteLayer(); void ExportLayer(); void ImPortLayer();
- Commands.cpp
#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(); } } } } }
二、字体样式表
2.1 字体样式表类
- 模板:以第(三)篇文件结构为基础,构建自己的类库
- 在创建SymbolTable筛选器中添加
- TextStyle.h
#pragma once #include <vector> class TextStyle { public: TextStyle(); ~TextStyle(); // 创建字体样式:字体样式名、字体文件名、大字体文件名(亚洲国家用) static AcDbObjectId Add(const ACHAR *name, const ACHAR *fontFileName, const ACHAR *bigfontFile); // 获取字体样式表记录id:字体样式名称 static AcDbObjectId GetStyleId(const ACHAR *styleName); };
- TextStyle.cpp
#include "stdafx.h" #include "TextStylel.h" TextStyle::TextStyle(){} TextStyle::~TextStyle(){} // 创建字体样式:字体样式名、字体文件名、大字体文件名(亚洲国家用) AcDbObjectId TextStyle::Add(const ACHAR *name, const ACHAR *fontFileName, const ACHAR *bigfontFile) { // 获得字体样式表 AcDbTextStyleTable *pTextStyle = NULL; acdbHostApplicationServices()->workingDatabase()-> getTextStyleTable(pTextStyle, AcDb::kForWrite); // 创建字体样式表记录 AcDbTextStyleTableRecord *pTextStyleRcd = new AcDbTextStyleTableRecord(); // 字体样式表记录添加:名称、字体文件名、比例、 pTextStyleRcd->setName(name); pTextStyleRcd->setBigFontFileName(bigfontFile); pTextStyleRcd->setFileName(fontFileName); pTextStyleRcd->setXScale(1.0); // 字体样式记录添加进字体样式表中 pTextStyle->add(pTextStyleRcd); // 返回字体样式表记录id AcDbObjectId styleId = pTextStyleRcd->objectId(); pTextStyleRcd->close(); pTextStyle->close(); return styleId; } // 获取字体样式表记录id:字体样式名称 AcDbObjectId TextStyle::GetStyleId(const ACHAR *styleName) { AcDbObjectId textStyleId = AcDbObjectId::kNull; // 如果字体样式名非空 if (_tcslen(styleName) > 0) { AcDbTextStyleTable *pTextStyle = NULL; // 获得字体样式表指针 acdbHostApplicationServices()->workingDatabase()-> getTextStyleTable(pTextStyle, AcDb::kForRead); // 获得字体样式记录id 并赋值textStyleId pTextStyle->getAt(styleName, textStyleId); pTextStyle->close(); } // 若字体样式名为空,返回kNull return textStyleId; }
2.2 测试代码
- Commands.h
#include "StdAfx.h" void AddCommands(); void CreateTextStyle();
- Commands.cpp
#include "StdAfx.h" #include "Commands.h" #include "Editor.h" #include "TextStylel.h" void AddCommands() { // 新建字体样式 Editor::AddCommand(L"Add", ACRX_CMD_MODAL, CreateTextStyle); } void CreateTextStyle() { CString textStyleName = _T("TESTSTYLE"); // 获取TESTSTYLE字体样式id AcDbObjectId txtstyleId = TextStyle::GetStyleId(textStyleName); // 若为空,则创建 if (txtstyleId.isNull()) { // 字体样式文件为CAD的fonts文件夹中的文件 TextStyle::Add(textStyleName, _T("txt.shx"), _T("hztxt.shx")); } }
- 效果
传送门 返回 列表