ObjectARX_符號表---->圖層

LAYER命令:圖層特性管理器:查看圖層指針

鎖定圖層: layiso ;解鎖圖層:layunisocode

建立圖層記錄:orm

void ZffCHAP4NewLayer()
{
// 提示用戶輸入新建圖層的名稱
char layerName[100];
if (acedGetString(Adesk::kFalse, "\n輸入新圖層的名稱:",layerName) != RTNORM)
   return;

// 得到當前圖形的層表
AcDbLayerTable *pLayerTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->getLayerTable(pLayerTbl, AcDb::kForWrite);

// 是否已經包含指定的層表記錄
if (pLayerTbl->has(layerName))
{
pLayerTbl->close();
return;
}

// 建立新的層表記錄
AcDbLayerTableRecord *pLayerTblRcd = new AcDbLayerTableRecord();
pLayerTblRcd->setName(layerName);

// 設置顏色,層的其餘屬性(線型等)都用缺省值
AcCmColor color; 
color.setColorIndex(1); 
pLayerTblRcd->setColor(color); 

// 將新建的層表記錄添加到層表中
AcDbObjectId layerTblRcdId;
pLayerTbl->add(layerTblRcdId, pLayerTblRcd);
acdbHostApplicationServices()->workingDatabase()->setClayer(layerTblRcdId);
pLayerTblRcd->close();
pLayerTbl->close();
}

修改指定圖層的顏色get

void ZffCHAP4LayerColor()
{
// 提示用戶輸入要修改的圖層名稱
char layerName[100];
if (acedGetString(Adesk::kFalse, "\n輸入圖層的名稱:",layerName) != RTNORM)
  return;

// 得到當前圖形的層表
AcDbLayerTable *pLayerTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->getLayerTable(pLayerTbl, AcDb::kForRead);

// 判斷是否包含指定名稱的層表記錄
if (!pLayerTbl->has(layerName))
{
pLayerTbl->close();
return;
}

// 得到指定層表記錄的指針
AcDbLayerTableRecord *pLayerTblRcd = NULL;
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();
}

刪除指定的圖層it

void ZffCHAP4DelLayer()
{
// 提示用戶輸入要修改的圖層名稱
char layerName[100];
if (acedGetString(Adesk::kFalse, "\n輸入圖層的名稱:",layerName) != RTNORM)
return;

// 得到當前圖形的層表
AcDbLayerTable *pLayerTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->getLayerTable(pLayerTbl, AcDb::kForRead);

// 判斷是否包含指定名稱的層表記錄
if (!pLayerTbl->has(layerName))
{
pLayerTbl->close();
return;
}

// 得到指定層表記錄的指針
AcDbLayerTableRecord *pLayerTblRcd = NULL;
pLayerTbl->getAt(layerName, pLayerTblRcd, AcDb::kForWrite);
pLayerTblRcd->erase();  // 爲其設置「刪除」標記
pLayerTblRcd->close();
pLayerTbl->close();
}

全部圖層及其特性導出到文本文件io

void ZffCHAP4ExportLayer()
{
// 建立所要導出的文本文件
CStdioFile f;
CFileException e;
char *pFileName = "C:\\layers.txt";
if (!f.Open(pFileName, CFile::modeCreate | CFile::modeWrite, &e))
{
acutPrintf("\n建立導出文件失敗!");
return;
}

// 得到層表指針
AcDbLayerTable *pLayerTbl = NULL;
acdbHostApplicationServices()->workingDatabase()->getLayerTable(pLayerTbl, AcDb::kForRead);

// 使用遍歷器訪問每一條層表記錄
AcDbLayerTableIterator *pItr = NULL;
AcDbLayerTableRecord *pLayerTblRcd = NULL;
pLayerTbl->newIterator(pItr);
for (pItr->start(); !pItr->done(); pItr->step())
{
pItr->getRecord(pLayerTblRcd, AcDb::kForRead);

// 輸出圖層的信息
CString strLayerInfo; // 圖層名稱
char *layerName = NULL;
pLayerTblRcd->getName(layerName);
strLayerInfo = layerName;
free(layerName);

strLayerInfo += ",";  // 分隔符
CString strColor; // 圖層顏色
AcCmColor color = pLayerTblRcd->color();
strColor.Format("%d", color.colorIndex());
strLayerInfo += strColor;

strLayerInfo += ",";
CString strLinetype; // 圖層線型
AcDbLinetypeTableRecord *pLinetypeTblRcd = NULL;
acdbOpenObject(pLinetypeTblRcd,pLayerTblRcd->linetypeObjectId(),AcDb::kForRead);
char *linetypeName = NULL;
pLinetypeTblRcd->getName(linetypeName);
pLinetypeTblRcd->close();
strLinetype = linetypeName;
free(linetypeName);
strLayerInfo += strLinetype;

strLayerInfo += ",";
CString strLineWeight;  // 圖層的線寬
AcDb::LineWeight lineWeight = pLayerTblRcd->lineWeight();
strLineWeight.Format("%d", lineWeight);
strLayerInfo += strLineWeight;

// 將圖層特性寫入到文件中
f.WriteString(strLayerInfo);
f.WriteString("\n");
pLayerTblRcd->close();
}
delete pItr;
pLayerTbl->close();
}

根據文本文件導入全部圖層及其特性class

//讀取一行文本以後,須要根據分隔符( 「,」 )來解析出圖層的名稱、顏色、線型和線寬
BOOL GetFieldText(CString strLineText, CStringArray &fields)
{ 
if (strLineText.Find(",", 0) == -1) // 若是找不到英文逗號,函
數退出
{
return FALSE;
} 
int nLeftPos = 0, nRightPos = 0; // 查找分隔符的起始位置
while ((nRightPos = strLineText.Find(",", nRightPos)) != -1)
{
fields.Add(strLineText.Mid(nLeftPos, nRightPos - nLeftPos));
nLeftPos = nRightPos + 1;
nRightPos++; 
}
// 最後一個列的數據
fields.Add(strLineText.Mid(nLeftPos));
return TRUE;
}

void ZffCHAP4ImportLayer()
{
// 打開所要導入的文本文件
CStdioFile f;
CFileException e;
char *pFileName = "C:\\layers.txt";
if (!f.Open(pFileName, CFile::modeRead, &e))
{
acutPrintf("\n打開導入文件失敗!");
return;
}

// 得到層表指針
AcDbLayerTable *pLayerTbl = NULL;
AcDbLayerTableRecord *pLayerTblRcd = NULL;
acdbHostApplicationServices()->workingDatabase()->getLayerTable(pLayerTbl, AcDb::kForWrite);

// 讀取文件中的每一行數據
CString strLineText;  // 一行文字
while (f.ReadString(strLineText))
{
// 跳過空行
if (strLineText.IsEmpty())
   continue;

// 解析出圖層名稱、顏色、線型和線寬
CStringArray layerInfos;
if (!GetFieldText(strLineText, layerInfos))
   continue;

// 建立新的層表記錄,或者打開存在的塊表記錄
AcDbLayerTableRecord *pLayerTblRcd = NULL;
AcDbObjectId layerTblRcdId;
if (pLayerTbl->has(layerInfos.GetAt(0)))
{
pLayerTbl->getAt(layerInfos.GetAt(0), layerTblRcdId);
}
else
{
pLayerTblRcd = new AcDbLayerTableRecord();
pLayerTblRcd->setName(layerInfos.GetAt(0));
pLayerTbl->add(layerTblRcdId, pLayerTblRcd);
pLayerTblRcd->close();
}
acdbOpenObject(pLayerTblRcd, layerTblRcdId,AcDb::kForWrite);

// 設置層表記錄的顏色
AcCmColor color;
Adesk::UInt16 colorIndex = atoi(layerInfos.GetAt(1));
color.setColorIndex(colorIndex);
pLayerTblRcd->setColor(color);

// 設置線型
AcDbLinetypeTable *pLinetypeTbl = NULL;
AcDbObjectId linetypeId;
acdbHostApplicationServices()->workingDatabase()->getLinetypeTable(pLinetypeTbl,AcDb::kForRead);
if (pLinetypeTbl->has(layerInfos.GetAt(2)))
{
pLinetypeTbl->getAt(layerInfos.GetAt(2), linetypeId);
}
else
{
pLinetypeTbl->getAt("Continous", linetypeId);
}
pLayerTblRcd->setLinetypeObjectId(linetypeId);
pLinetypeTbl->close();

// 設置線寬
AcDb::LineWeight lineWeight =(AcDb::LineWeight)atol(layerInfos.GetAt(3)); 
pLayerTblRcd->setLineWeight(lineWeight);
pLayerTblRcd->close();
}
pLayerTbl->close();
}
相關文章
相關標籤/搜索