|
[code]为了更好的维护和扩展代码, 博客(1)中的类CCreateLine修改为CCreateEnt,类CModifyLine修改为CModifyEnt,类的功能扩展为创建和修改实体,而不单单是对直线的操作。
(1) 类CCreateEnt
//CreateEnt.h
#pragma once
#include "StdAfx.h"
class CCreateEnt
{
public:
CCreateEnt(void);
~CCreateEnt(void);
static AcDbObjectId CreateLine(AcGePoint3d ptStart, AcGePoint3d ptEnd); //函数声明
static AcDbObjectId PostToModelSpace(AcDbEntity* pEnt);
};
//CreateEnt.cpp
#include "stdafx.h"
#include "CreateEnt.h"
CCreateEnt::CCreateEnt(void)
{
}
CCreateEnt::~CCreateEnt(void)
{
}
//创建直线
AcDbObjectId CCreateEnt::CreateLine(AcGePoint3d ptStart, AcGePoint3d ptEnd) //函数实现
{
AcDbLine *pLine = new AcDbLine(ptStart, ptEnd);
//将实体添加到图形数据库
AcDbObjectId lineId;
lineId = CCreateEnt::PostToModelSpace(pLine);
return lineId;
}
// 将实体添加到图形数据库的模型空间
AcDbObjectId CCreateEnt::PostToModelSpace(AcDbEntity* pEnt)
{
// 获得指向块表的指针
AcDbBlockTable *pBlockTable;
acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable, AcDb::kForRead);
// 获得指向特定的块表记录(模型空间)的指针
AcDbBlockTableRecord *pBlockTableRecord;
pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite);
// 将AcDbLine类的对象添加到块表记录中
AcDbObjectId entId;
pBlockTableRecord->appendAcDbEntity(entId, pEnt);
// 关闭图形数据库的各种对象
pBlockTable->close();
pBlockTableRecord->close();
pEnt->close();
return entId;
}
(2) 类CModifyEnt
//ModifyEnt.h
#pragma once
#include "StdAfx.h"
class CModifyEnt
{
public:
CModifyEnt(void);
~CModifyEnt(void);
Acad::ErrorStatus ChangeColor(AcDbObjectId entId, Adesk::UInt16 colorIndex);
};
//ModifyEnt.cpp
#include "stdafx.h"
#include "ModifyEnt.h"
CModifyEnt::CModifyEnt(void)
{
}
CModifyEnt::~CModifyEnt(void)
{
}
//改变颜色的直线
Acad::ErrorStatus CModifyEnt::ChangeColor(AcDbObjectId entId, Adesk::UInt16 colorIndex)
{
AcDbEntity *pEntity;
// 打开图形数据库中的对象
acdbOpenObject(pEntity, entId, AcDb::kForWrite);
// 修改实体的颜色
pEntity->setColorIndex(colorIndex);
// 用完之后,及时关闭
pEntity->close();
return Acad::eOk;
}
(3) acrxEntryPoint.cpp
//当前项目中注册一个命令 AddLine
static void MidasMyGroupMyChangeColor()
{
AcGePoint3d ptStart(0, 0, 0);
AcGePoint3d ptEnd(10, 10, 0);
AcDbObjectId lineId;
lineId = CCreateEnt::CreateLine(ptStart, ptEnd);
CModifyEnt m_modifyEnt;
m_modifyEnt.ChangeColor(lineId, 1);
}
ACED_ARXCOMMAND_ENTRY_AUTO(CArxConfigApp, MidasMyGroup, MyChangeColor, MyChangeColor, ACRX_CMD_MODAL, NULL) //画直线
原文链接:https://blog.csdn.net/qq_42981953/article/details/121793694[/code] |
|