ObjectARX模仿AutoCAD中多段线命令实现颜色,线型,闭合等 - 小众知识

ObjectARX模仿AutoCAD中多段线命令实现颜色,线型,闭合等

2013年01月27日 14:18:05 苏内容
  标签: ObjectARX/AutoCAD
阅读:597

首先在acrxEntryPoint.cpp中注册一个命令:

IMPLEMENT_ARX_ENTRYPOINT(CARX_CreatePolylineApp)
ACED_ARXCOMMAND_ENTRY_AUTO(CARX_CreatePolylineApp, HH, AddPoly, AddPoly, ACRX_CMD_TRANSPARENT, NULL)

该命令的具体实现如下:

static void HHAddPoly(void)
	{		int nColorIndex = 0; // 颜色索引值
		ads_real width = 0;  // 多段线的线宽
		int nIndex = 2;  // 当前输入点的次数
		ads_point ptStart; // 起点
		ads_point ptPrevious;// 前一个参考点
		ads_point ptCurrent; //当前拾取的点
		AcDbObjectId polyId; //多段线ID
	
		// 提示用户输入起点
		if (RTNORM != acedGetPoint(NULL, _T("\n输入第一点:"), ptStart))
		{			return ;
		}		
		// 输入第二点
		acedInitGet(NULL, _T("W C B F"));		int rc = acedGetPoint(ptStart,_T("\n输入下一点 [宽度(W)/颜色(C)/闭合(B)]<完成(F)>:"), ptCurrent);		while (RTNORM == rc || RTKWORD == rc)
		{			if (RTKWORD == rc) // 如果用户输入了关键字
			{
				ACHAR keyWord[20];// 关键字
				//获取输入的关键字
				if (RTNORM != acedGetInput(keyWord))
				{					return;
				}				if (0 == _tcscmp(keyWord, _T("W")))
				{
					width = CAuxiliary::GetWidth();
					AcDbPolyline *pPoly = CAuxiliary::GetPolyLine(polyId);					if (NULL == pPoly)
					{						return;
					}
					pPoly->setConstantWidth(width);
					pPoly->close();
				}				else if (0 == _tcscmp(keyWord, _T("C")))
				{
					nColorIndex = CAuxiliary::GetColorIndex();
					AcDbPolyline *pPoly = CAuxiliary::GetPolyLine(polyId);					if (NULL == pPoly)
					{						return;
					}
					pPoly->setColorIndex(nColorIndex);
					pPoly->close();
					
				}				else if (0 == _tcscmp(keyWord, _T("B")))
				{					if (nIndex < 3)
					{
						acutPrintf(_T("\n输入点个数不足,不能执行闭合操作"));						return;
					}

					AcDbPolyline *pPoly = CAuxiliary::GetPolyLine(polyId);					if (NULL == pPoly)
					{						return;
					}
					pPoly->setClosed(Adesk::kTrue);
					pPoly->close();					return;
				}				else if (0 == _tcscmp(keyWord, _T("F")))
				{					return;
				}				else
				{
					acutPrintf(_T("\n无效的关键字."));
				}
			}			else if (RTNORM == rc)  // 用户输入了点
			{
				acutPrintf(_T("\n输入点的坐标是(%.2f, %.2f, %.2f)"), ptCurrent[X], ptCurrent[Y], ptCurrent[Z]);				
				if (2 == nIndex)
				{					// 创建多段线
					polyId = CAuxiliary::CreatePolyline(ptStart, ptCurrent, width, nColorIndex);
				}				else if (nIndex > 2)
				{					//修改多段线
					CAuxiliary::AddPolyline(polyId, ptCurrent,nIndex, width, nColorIndex);
				}	

				++nIndex;
				acdbPointSet(ptCurrent, ptPrevious);//acdbPointSet宏: ads_point 变量值的复制
			}			// 提示用户输入新的节点
			acedInitGet(NULL, _T("W C B F"));
			rc = acedGetPoint(ptPrevious, _T("\n输入下一点 [宽度(W)/颜色(C)/闭合(B)]<完成(F)>:"), ptCurrent);
		 }
	}

其中调用了一些函数,新建一个类CAuxiliary,具体实现如下:

***********************Auxiliary.h********************#pragma onceclass CAuxiliary{public:
	CAuxiliary(void);
	~CAuxiliary(void);	//图形数据库
	static bool PostToModelSpace(AcDbEntity* pEnt, AcDbObjectId &entId);	//通过ID获取对象
	static AcDbPolyline * GetPolyLine(AcDbObjectId polyId);	//创建仅包含一条直线的多段线
	static AcDbObjectId CreatePolyline(ads_point ptStart, ads_point ptEnd, double width, int nColorIndex);	//添加一条直线到多段线
	static void AddPolyline(AcDbObjectId polyId, ads_point ptCurrent,int nIndex, double width, int nColorIndex);	
	//获取用户输入的线宽
	static ads_real GetWidth();	//提示用户输入颜色索引值
	static int GetColorIndex();
};

 

***********************Auxiliary.cpp********************#include "StdAfx.h"#include "Auxiliary.h"#include <atlstr.h>#include <tchar.h>CAuxiliary::CAuxiliary(void)
{
}

CAuxiliary::~CAuxiliary(void)
{
}bool CAuxiliary::PostToModelSpace(AcDbEntity* pEnt, AcDbObjectId &entId)
{	if (NULL == pEnt)
	{		return false;
	}	// 获得指向块表的指针
	AcDbBlockTable *pBlockTable = NULL;	//workingDatabase()能够获得一个指向当前活动的图形数据库的指针,
	acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable, AcDb::kForRead);	if (NULL == pBlockTable)
	{		return false;
	}	// 获得指向特定的块表记录(模型空间)的指针
	AcDbBlockTableRecord *pBlockTableRecord = NULL;
	pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord,AcDb::kForWrite);	if (NULL == pBlockTableRecord)
	{		return false;
	}	// 将AcDbLine类的对象添加到块表记录中
	pBlockTableRecord->appendAcDbEntity(entId, pEnt); 

	// 关闭图形数据库的各种对象
	pBlockTable->close();
	pBlockTableRecord->close();
	pEnt->close(); 

	return true;
}

AcDbPolyline * CAuxiliary::GetPolyLine(AcDbObjectId polyId)
{	AcDbPolyline * pPoly(NULL);	if (Acad::eOk != acdbOpenObject(pPoly, polyId, AcDb::kForWrite))
	{		return NULL;
	}	return pPoly;
}

AcDbObjectId CAuxiliary::CreatePolyline(ads_point ptStart, ads_point ptCurrent, double width,int nColorIndex)
{
	AcDbPolyline *pPoly = new AcDbPolyline;

	AcGePoint2d ptInsert = asPnt2d(ptStart);
	AcGePoint2d ptInsert1 = asPnt2d(ptCurrent);

	pPoly->addVertexAt(0, ptInsert, nColorIndex, width, width);
	pPoly->addVertexAt(1, ptInsert1, nColorIndex, width, width);

	AcDbObjectId polyId;	if (!CAuxiliary::PostToModelSpace(pPoly, polyId))
	{
		acutPrintf(_T("\n加入图形数据库失败"));		return NULL;
	}	return polyId;
}void CAuxiliary::AddPolyline(AcDbObjectId polyId, ads_point ptCurrent,int nIndex, double width, int nColorIndex)
{
	AcDbPolyline *pPoly = GetPolyLine(polyId);	if (NULL == pPoly)
	{		return;
	}
	AcGePoint2d ptInsert = asPnt2d(ptCurrent);
	pPoly->addVertexAt(nIndex - 1, ptInsert, nColorIndex, width, width);	
	pPoly->close();
}
ads_real CAuxiliary::GetWidth()
{
	ads_real width = 0;	if (RTNORM == acedGetReal(_T("\n输入线宽:"), &width))
	{		return width;
	}	else
	{		return 0;
	}
}int CAuxiliary::GetColorIndex()
{	int nColorIndex = 0;	if (RTNORM != acedGetInt(_T("\n输入颜色索引值(0~256):"), &nColorIndex))
	{		return 0;
	}	// 处理颜色索引值无效的情况
	while (nColorIndex < 0 || nColorIndex > 256)
	{
		acedPrompt(_T("\n输入了无效的颜色索引."));		if (RTNORM != acedGetInt(_T("\n输入颜色索引值(0~256):"), &nColorIndex))
		{			return 0;
		}
	}	return nColorIndex;
}


扩展阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1