ObjectARX模仿AutoCAD中多段線命令實現顏色,線型,閉合等

首先在acrxEntryPoint.cpp中註冊一個命令:數據庫

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

該命令的具體實現以下:app

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 once

class 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;
}
相關文章
相關標籤/搜索