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

简介: 导出图层的实现: static void qxzyOperateLayer_ExportLayer(void) { CStdioFile f; CFileException e; char *pFileName = "C:\\layers.txt"; if(!f.Open((LPCTSTR)pFileName, CFile::modeCreate|CFile::mod

导出图层的实现:

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;
	}


目录
相关文章
|
5月前
|
BI
Qt 报表实现(二)----QtXlsx
Qt 报表实现(二)----QtXlsx
184 2
Altium Designer导出Gerber文件的步骤
在设计好PCB之后就需要发出去制作,一般我们都会选择发送Gerber文件。当然也可以发送PCB原文件,只不过这样没有保密性,这个根据自己的实际情况来选择。如果PCB对于保密性没有要求,自己对工艺又不是很了解的话,为了方便可以直接发送PCB原文件。这里还是建议大家导出Gerber文件做比较好,下面我们就来看一下Altium Designer如何导出Gerber文件。
241 0
|
2月前
|
开发框架 前端开发 JavaScript
在Winform分页控件中集成导出PDF文档的功能
在Winform分页控件中集成导出PDF文档的功能
|
5月前
MFC编程 -- 高级列表框添加数据
MFC编程 -- 高级列表框添加数据
43 0
|
数据可视化 定位技术
地图可视化开发技巧:geojson转svg后再转emf格式插入ppt实现编辑的解决方案
地图可视化开发技巧:geojson转svg后再转emf格式插入ppt实现编辑的解决方案
183 0
|
5月前
|
Python
ArcGIS中ArcMap通过Python程序脚本新建工具箱与自定义工具的方法
ArcGIS中ArcMap通过Python程序脚本新建工具箱与自定义工具的方法
|
5月前
|
编解码 定位技术 Python
Python中ArcPy实现ArcGIS自动批量制图与地图要素批量设置
Python中ArcPy实现ArcGIS自动批量制图与地图要素批量设置
161 1
【ArcGIS教程】ArcMap中如何导入外部Excel属性数据呢?
【ArcGIS教程】ArcMap中如何导入外部Excel属性数据呢?
【ArcGIS教程】ArcMap中如何导入外部Excel属性数据呢?
|
BI 测试技术 数据库
Ireport报表插件使用之二——table组件(Ireport5.6.0版本)
Ireport如何使用table组件,其中list,子表,交叉表也是这种用法
|
移动开发 C#
C# 添加、修改以及删除Excel迷你图表的方法
Excel表格中的迷你图表能够直观地向我们展示出数据的变化趋势。本文将介绍C#如何实现为表格数据生成迷你图表,以及修改和删除迷你图表的方法。下面将详细讲述。 所用组件工具:Spire.XLS for .
1244 0