ObjectArx学习笔记-导入导出图层列表

导出图层的实现:

static void qxzyOperateLayer_ExportLayer(void)
{
CStdioFile f;
CFileException e;
char *pFileName = "C:\\layers.txt";
if(!f.Open((LPCTSTR)pFileName, 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;
ACHAR *layerName;
pLayerTblRcd->getName(layerName);
strLayerInfo = layerName;
free(layerName);
strLayerInfo += ",";

CString strColor;
AcCmColor color = pLayerTblRcd->color();
strColor.Format(_T("%d"), color.colorIndex());
strLayerInfo += strColor;
strLayerInfo += ",";

CString strLinetype;
AcDbLinetypeTableRecord *pLinetypeTblRcd;
acdbOpenObject(pLinetypeTblRcd, pLayerTblRcd->linetypeObjectId(),
AcDb::kForRead);
ACHAR *linetypeName;
pLinetypeTblRcd->getName(linetypeName);
pLinetypeTblRcd->close();
strLinetype = linetypeName;
free(linetypeName);
strLayerInfo += strLinetype;
strLayerInfo += ",";

CString strLineWeight;
AcDb::LineWeight lineWeight = pLayerTblRcd->lineWeight();
strLineWeight.Format(_T("%d"),lineWeight);
strLayerInfo += strLineWeight;

f.WriteString(strLayerInfo);
f.WriteString(_T("\n"));

pLayerTblRcd->close();
}
delete pItr;
pLayerTbl->close();
}

导入图层的实现:
static void qxzyOperateLayer_ImportLayer(void)
{
CStdioFile f;
CFileException e;
char *pFileName = "C:\\layers.txt";
if(!f.Open((LPCTSTR)pFileName, CFile::modeRead, &e))
{
acutPrintf(_T("\n打开导入文件失败!"));
return;
}

AcDbLayerTable *pLayerTbl;
AcDbLayerTableRecord *pLayerTblRcd;
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;
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((LPSTR)(LPCTSTR)layerInfos.GetAt(1));
color.setColorIndex(colorIndex);
pLayerTblRcd->setColor(color);

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

AcDb::LineWeight lineWeight = (AcDb::LineWeight)atoi((LPSTR)(LPCTSTR)layerInfos.GetAt(3));
pLayerTblRcd->setLineWeight(lineWeight);
pLayerTblRcd->close();
}
pLayerTbl->close();

}

获取字段的一个方法:
static BOOL GetFieldText(CString strLineText, CStringArray &fields)
{
if(strLineText.Find(_T(","), 0) == -1)
{
return FALSE;
}

int nLeftPos = 0, nRightPos = 0;

while((nRightPos = strLineText.Find(_T(","), nRightPos))!= -1)
{
fields.Add(strLineText.Mid(nLeftPos, nRightPos - nLeftPos));

nLeftPos = nRightPos + 1;
nRightPos ++;
}

fields.Add(strLineText.Mid(nLeftPos));

return TRUE;
}

posted @ 2020-03-01 22:36  中国膜结构网mjgou  阅读(383)  评论(0编辑  收藏  举报