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

转载:http://blog.csdn.net/foreverling/article/details/28266181

时间: 2024-09-13 10:49:31

ObjectArx学习笔记-导入导出图层列表的相关文章

ObjectArx学习笔记-获取某一图层上所有直线

//----------------------------------------------------------------------------- //----- acrxEntryPoint.h //----------------------------------------------------------------------------- #include "StdAfx.h" #include "resource.h" #include

ObjectArx学习笔记-Object 2013 Wizard问题解决

AutoCAD ObjectArx 2013 Wizard 可以到Autodesk的官网下载. 1.安装完成后,在Visual Studio中使用Wizard提供的模版新建工程时,总是提示"编码不正确,无法创建工程".折腾好久,找到问题:  C:\Program Files (x86)\Autodesk\ObjectARX2013 Wizards\Autodesk.arx-2013.props C:\Program Files (x86)\Autodesk\ObjectARX 2013

ObjectArx学习笔记-选择文件及ACHAR用法

ObjectArx中实现文件选择的代码如下: static void qxzyGetFileD_SelectFile(void) { // Add your code for command qxzyGetFileD._SelectFile here const ACHAR* title = L"选择图形文件"; const ACHAR* path = L"C:\\"; struct resbuf *fileName; fileName = acutNewRb(RT

ObjectArx学习笔记-画线

一.安装vs2005 二.安装AutoCAD2008 三.安装ObjectArx2008 四.建立第一个ObjectArx程序 1.首先用ObjectARX Commands添加一个命令 然后在acrxEntryPoint.cpp中生成如下代码: ACED_ARXCOMMAND_ENTRY_AUTO(CArxProject1App, qxzyArxProject1, _MyCommand1, MyCommand1, ACRX_CMD_TRANSPARENT, NULL) 以及: // - qxz

ObjectArx学习笔记-画线并修改颜色改进写法

1.创建工程CreateEnts 2.添加类CCreateEnt,在文件CreateEnt.h中添加函数声明,在CreateEnt.cpp添加函数实现代码: static AcDbObjectId CreateLine(); AcDbObjectId CCreateEnt::CreateLine() { AcGePoint3d ptStart(0,0,0); AcGePoint3d ptEnd(100,100,0); AcDbLine *pLine = new AcDbLine(ptStart,

ObjectArx学习笔记-画线并修改颜色

实现画线和修改颜色全部代码: // (C) Copyright 2002-2005 by Autodesk, Inc. // // Permission to use, copy, modify, and distribute this software in // object code form for any purpose and without fee is hereby granted, // provided that the above copyright notice appe

Yii学习笔记之CGridView数据列表添加复选框提供多选批量删除、更新等功能

Yii框架是一个很方便又好用的框架,就是文档上不是那么的完善,有很多东西需要挖掘代码. 近日,使用Yii框架开发的时候,就遇上了这么一些问题,需要为CGridView列表提供复选框功能,方便同时批量更新多条数据.以下是步骤: 1. columns中增加一列放置checkbox 2. widget下添加div放置操作按钮 3. 添加CheckAll和Ajax提交数据 完整代码如下: <?php $this->widget('zii.widgets.grid.CGridView', array(

ObjectArx学习笔记-设置字体样式

实现代码: static void qxzyOperateLayer_AddStyle(void) { AcDbTextStyleTable *pTextStyleTbl; acdbHostApplicationServices()->workingDatabase() ->getTextStyleTable(pTextStyleTbl, AcDb::kForWrite); AcDbTextStyleTableRecord *pTextStyleTblRcd; pTextStyleTblRcd

ObjectArx学习笔记-acedGetXXX

acedGetInt函数暂停程序执行,等待用户输入一个整数.该函数定义为: int acedGetInt(const char * prompt, int * result); prompt用于指定显示在命令窗口中的字符串,如果不需要使用可以指定NULL作为该参数的值. acedGetReal函数暂停程序执行,等待用户输入一个实数. acedGetString函数暂停程序执行,等待用户输入一个字符串,该函数定义为: int acedGetString(int cronly, const char