2012-04-13 22:36:00|?次阅读|上传:wustguangh【已有?条评论】发表评论
关键词:ObjectArx, C/C++, AutoCAD|来源:唯设编程网
图层操作是ObjectArx2010对AutoCAD2010进行开发的一个重点,对AutoCAD2010有一定使用经验的用户都知道,AutoCAD2010默认的图层几乎总是无法满足实际需要,我们得定义自己的图层,那么如何使用ObjectArx2010完成图层的定制呢?网上各大论坛有很多答案,本文将其进行总结,主要包括使用ObjectArx2010创建图层,删除一个已经存在的图层,修改图层的属性等图层操作相关的内容,为方便理解,本文将给出一个相对完整的图层操作工具类实现相关功能,读者可以直接使用其中的全部或者部分源代码。
首先定义一个图层信息类,
#include <sstream>
/**
图层参数的类
**/
class LayerInfo{
public:
//图层名称
std::wstring layerName;
//颜色序号
Adesk::UInt16 colorIndex;
//线型
std::wstring typeName;
//线宽
AcDb::LineWeight weight;
public:
LayerInfo();
LayerInfo(ACHAR* layerName,
Adesk::UInt16 color,
ACHAR* ltype,AcDb::LineWeight w);
//格式化成字符串
CString ToString();
//格式化标题
static CString FormatTitle();
//通过字符串实例化一个图层
static LayerInfo LoadByStr(CString line);
};
LayerInfo类的目的是方便图层信息的操作,同时实现了将图层信息格式化成预设格式的字符串,格式化自定义的标题行以及从文件行实例化一个图层信息类的功能接口,LayerInfo的完整实现如下:
LayerInfo::LayerInfo():
layerName(_T("")),
colorIndex(0),
typeName(_T("")),
weight(AcDb::LineWeight::kLnWt000){
}
LayerInfo::LayerInfo(ACHAR* layerName,
Adesk::UInt16 color,
ACHAR* ltype,
AcDb::LineWeight w):
layerName(layerName),
colorIndex(color),
typeName(ltype),
weight(w)
{
}
//格式化成字符串
CString LayerInfo::ToString(){
CString strLayer;
strLayer.Format(_T("%-20s%-20d%-20s%-20d"),
layerName.c_str(),
colorIndex,
typeName.
c_str(),
weight);
//strLayer.Format(_T("%s%d%s%d"),"0",7,"Continuous",-3);
return strLayer;
}
//格式化标题
CString LayerInfo::FormatTitle(){
CString strTitle;
strTitle.Format(_T("%-20s%-20s%-20s%-20s"),
_T("layerName"),
_T("colorIndex"),
_T("typeName"),
_T("weight"));
return strTitle;
}
//通过字符串实例化一个图层
LayerInfo LayerInfo::LoadByStr(CString line){
LayerInfo layer;
std::wstring str(line.GetBuffer());
std::wistringstream input_istring(str);
int lineWeight=0;
input_istring
>>layer.layerName
>>layer.colorIndex
>>layer.typeName
>>lineWeight;
layer.weight=static_cast<AcDb::LineWeight>(lineWeight);
//释放缓存区
line.ReleaseBuffer();
return layer;
}
现在开始设计图层操作工具类CLayerOperator,根据前面的讨论,CLayerOperator需要完成的功能包括:a.定义一个新图层;b.删除图层;c.编辑图层的属性,包括线宽,颜色,线型;在工具类CLayerOperator中,我们还增加了将当前图形导出到文件和从文件导入到模型空间的功能。CLayerOperator的完整定义如下:
/**
图层操作的工具类
**/
class CLayerOperator
{
public:
CLayerOperator(void);
~CLayerOperator(void);
// 新建一个图层
static void NewLayer(ACHAR* layerName);
// 使用图形对话框设置指定图层的颜色
static void SetLayerColor(ACHAR* layerName);
// 删除一个图层
static void DelLayer(ACHAR* layerName);
// 导出图层到文件中
static void ExportLayers(ACHAR* fileName);
// 从文件导入图层
static void ImportLayers(ACHAR* fileName);
// 采用命令方式加载一种新的线型
static bool _CMDLoadLineType(ACHAR* linetype);
//加载一种新的线型
static AcDbObjectId LoadLineType(const ACHAR* linetype,
const ACHAR* fileName=_T("acadiso.lin"),
AcDbDatabase* pDb=acdbCurDwg());
//取得线型ID
//输入参数:AcDbDatabase* pDb, 数据库
// const ACHAR *LineName 线型名称
static AcDbObjectId CLayerOperator::GetLineId(const ACHAR *LineName,
AcDbDatabase* pDb=acdbCurDwg());
//加载图层到图形数据库
static void CLayerOperator::LoadLayer(const LayerInfo& layerInfo,
AcDbDatabase* pDb=acdbCurDwg());
};
下面讨论CLayerOperator类的具体实现。