首先看一下效果图:
分为4部分,先看代码最少的计算模块:
**********Calculation.h*******************#pragma onceclass sAcGePoint2d;class AcGePoint3d;class CCalculation{public: CCalculation(void); ~CCalculation(void); //计算两点连线的中点 static AcGePoint2d MiddlePoint(AcGePoint2d pt1, AcGePoint2d pt2); static AcGePoint3d MiddlePoint(AcGePoint3d pt1, AcGePoint3d pt2); static AcGePoint3d Pt2dTo3d(AcGePoint2d pt); //计算常量π的值 static double PI();};
**********Calculation.cpp*******************#include "StdAfx.h"#include "Calculation.h"#includeCCalculation::CCalculation(void){}CCalculation::~CCalculation(void){}AcGePoint2d CCalculation::MiddlePoint(AcGePoint2d pt1, AcGePoint2d pt2){ AcGePoint2d pt; pt[X] = (pt1[X] + pt2[X]) / 2; pt[Y] = (pt1[Y] + pt2[Y]) / 2; return pt;}AcGePoint3d CCalculation::MiddlePoint(AcGePoint3d pt1, AcGePoint3d pt2){ AcGePoint3d pt; pt[X] = (pt1[X] + pt2[X]) / 2; pt[Y] = (pt1[Y] + pt2[Y]) / 2; pt[Z] = (pt1[Z] + pt2[Z]) / 2; return pt;}AcGePoint3d CCalculation::Pt2dTo3d(AcGePoint2d pt){ AcGePoint3d ptTemp(pt.x, pt.y, 0); return ptTemp;}double CCalculation::PI(){ return 4 * atan(1.0);//反正切}
然后我们看一下,修改颜色,图层,线条:
**********CModifyEnt.h*******************#pragma once#include "StdAfx.h"class CModifyEnt{public: CModifyEnt(void); ~CModifyEnt(void); //改变颜色 static Acad::ErrorStatus ChangeColor(AcDbObjectId entId, Adesk::UInt16 colorIndex); //改变图层 static Acad::ErrorStatus ChangeLayer(AcDbObjectId entId,CString strLayerName); //改变线型 static Acad::ErrorStatus ChangeLinetype(AcDbObjectId entId,CString strLinetype);};
**********CModifyEnt.cpp*******************#include "StdAfx.h"#include "ModifyEnt.h"CModifyEnt::CModifyEnt(void){}CModifyEnt::~CModifyEnt(void){}Acad::ErrorStatus CModifyEnt::ChangeColor(AcDbObjectId entId, Adesk::UInt16 colorIndex){ AcDbEntity *pEntity = NULL; // 打开图形数据库中的对象 acdbOpenObject(pEntity, entId, AcDb::kForWrite); // 修改实体的颜 pEntity->setColorIndex(colorIndex);// colorIndex:0~256 的值,其中 0 代表随块,256 代表随层 // 用完之后,及时关闭 pEntity->close(); return Acad::eOk;}Acad::ErrorStatus CModifyEnt::ChangeLayer(AcDbObjectId entId, CString strLayerName){ AcDbEntity *pEntity = NULL; // 打开图形数据库中的对象 acdbOpenObject(pEntity, entId, AcDb::kForWrite); // 修改实体的图层 pEntity->setLayer(strLayerName); // 用完之后,及时关闭 pEntity->close(); return Acad::eOk;}Acad::ErrorStatus CModifyEnt::ChangeLinetype(AcDbObjectId entId, CString strLinetype){ AcDbEntity *pEntity = NULL; // 打开图形数据库中的对象 acdbOpenObject(pEntity, entId, AcDb::kForWrite); // 修改实体的线型 pEntity->setLayer(strLinetype); // 用完之后,及时关闭 pEntity->close(); return Acad::eOk;}
接下来,我们看最重要的一部分,创建直线,创建圆(4种方法),创建圆弧(5种方法):
**********CreateEnt.h*******************#pragma once#include "StdAfx.h"class CCreateEnt{public: CCreateEnt(void); ~CCreateEnt(void); // 将实体添加到图形数据库的模型空间 static AcDbObjectId PostToModelSpace(AcDbEntity* pEnt); // 创建一条线 static AcDbObjectId CreateLine(AcGePoint3d ptStart,AcGePoint3d ptEnd); //创建一个圆(圆心、半径法)(三维) // ptCenter:圆心; //radius:半径; //vec:圆所在平面 static AcDbObjectId CreateCircle(AcGePoint3d ptCenter, AcGeVector3d vec, double radius); //创建位于 XOY 平面上的圆(二维)依赖三维法 static AcDbObjectId CreateCircle(AcGePoint3d ptCenter, double radius); //创建一个圆(俩点法)依赖二维法 static AcDbObjectId CreateCircle(AcGePoint2d pt1, AcGePoint2d pt2); //创建一个圆(三点法)依赖二维法 static AcDbObjectId CreateCircle(AcGePoint2d pt1, AcGePoint2d pt2,AcGePoint2d pt3); //创建圆弧(圆心、半径、圆弧所在的平面、起点角度和终点角度) //ptCenter:圆心; //vec:圆弧所在平面 //radius:半径; //startAngle:起点角度 //endAngle:终点角度 static AcDbObjectId CreateArc(AcGePoint3d ptCenter, AcGeVector3d vec, double radius, double startAngle, double endAngle); //创建圆弧(二维)依赖于圆弧1法 static AcDbObjectId CreateArc(AcGePoint2d ptCenter, double radius, double startAngle, double endAngle); //创建圆弧(三点法)依赖于二维法 static AcDbObjectId CreateArc(AcGePoint2d ptStart, AcGePoint2d ptOnArc, AcGePoint2d ptEnd); //创建圆弧(起点、圆心、终点)依赖于二维法 static AcDbObjectId CreateArcSCE(AcGePoint2d ptStart, AcGePoint2d ptCenter, AcGePoint2d ptEnd); //创建圆弧(起点、圆心、圆弧角度)依赖于二维法 static AcDbObjectId CreateArc(AcGePoint2d ptStart, AcGePoint2d ptCenter, double angle);};
**********Calculation.cpp*******************#include "StdAfx.h"#include "CreateEnt.h"#include "Calculation.h"#include#include "gearc3d.h"CCreateEnt::CCreateEnt(void){}CCreateEnt::~CCreateEnt(void){}AcDbObjectId CCreateEnt::CreateLine(AcGePoint3d ptStart,AcGePoint3d ptEnd){ // 在内存上创建一个新的AcDbLine对象 //AcGePoint3d ptStart(0, 0, 0); //AcGePoint3d ptEnd(100, 100, 0); AcDbLine *pLine = new AcDbLine(ptStart, ptEnd); AcDbObjectId lineId; lineId = CCreateEnt::PostToModelSpace(pLine); return lineId;}AcDbObjectId CCreateEnt::PostToModelSpace(AcDbEntity* pEnt){ // 获得指向块表的指针 AcDbBlockTable *pBlockTable = NULL; //workingDatabase()能够获得一个指向当前活动的图形数据库的指针, acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable, AcDb::kForRead); // 获得指向特定的块表记录(模型空间)的指针 AcDbBlockTableRecord *pBlockTableRecord NULL; pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord,AcDb::kForWrite); // 将AcDbLine类的对象添加到块表记录中 AcDbObjectId entId; pBlockTableRecord->appendAcDbEntity(entId, pEnt); // 关闭图形数据库的各种对象 pBlockTable->close(); pBlockTableRecord->close(); pEnt->close(); return entId;}AcDbObjectId CCreateEnt::CreateCircle(AcGePoint3d ptCenter, AcGeVector3d vec, double radius){ AcDbCircle *pCircle = new AcDbCircle(ptCenter, vec, radius); // 将实体添加到图形数据库 AcDbObjectId circleId; circleId = CCreateEnt::PostToModelSpace(pCircle); return circleId;}AcDbObjectId CCreateEnt::CreateCircle(AcGePoint3d ptCenter, double radius){ AcGeVector3d vec(0, 0, 1); return CCreateEnt::CreateCircle(ptCenter, vec, radius);}AcDbObjectId CCreateEnt::CreateCircle(AcGePoint2d pt1, AcGePoint2d pt2){ // 计算圆心和半径 AcGePoint2d pt = CCalculation::MiddlePoint(pt1, pt2); AcGePoint3d ptCenter(pt[X], pt[Y], 0); // 圆心 double radius = pt1.distanceTo(pt2) / 2;//distanceTo 函数用于计算两点之间的距离,也就是半径 // 创建圆 return CCreateEnt::CreateCircle(ptCenter, radius);}AcDbObjectId CCreateEnt::CreateCircle(AcGePoint2d pt1, AcGePoint2d pt2,AcGePoint2d pt3){ /* // 使用数学方法 double xysm, xyse, xy; AcGePoint3d ptCenter; double radius; xy = pow(pt1[X], 2) + pow(pt1[Y], 2); xyse = xy - pow(pt3[X], 2) - pow(pt3[Y], 2); xysm = xy - pow(pt2[X], 2) - pow(pt2[Y], 2); xy = (pt1[X] - pt2[X]) * (pt1[Y] - pt3[Y]) - (pt1[X] - pt3[X]) * (pt1[Y] - pt2[Y]); // 判断参数有效性 if (fabs(xy) < 0.000001) { AfxMessageBox(_T("所输入的参数无法创建圆形!")); return 0; } // 获得圆心和半径 ptCenter[X] = (xysm * (pt1[Y] - pt3[Y]) - xyse * (pt1[Y] - pt2[Y])) / (2 * xy); ptCenter[Y] = (xyse * (pt1[X] - pt2[X]) - xysm * (pt1[X] - pt3[X])) / (2 * xy); ptCenter[Z] = 0; radius = sqrt((pt1[X] - ptCenter[X]) * (pt1[X] - ptCenter[X]) + (pt1[Y] - ptCenter[Y]) * (pt1[Y] - ptCenter[Y])); if (radius < 0.000001) { AfxMessageBox(_T("半径过小!")); return 0; } return CCreateEnt::CreateCircle(ptCenter, radius);*/ // 使用几何类 AcGeCircArc2d geArc(pt1, pt2, pt3); AcGePoint3d ptCenter(geArc.center().x, geArc.center().y, 0); return CCreateEnt::CreateCircle(ptCenter, geArc.radius()); }AcDbObjectId CCreateEnt::CreateArc(AcGePoint3d ptCenter, AcGeVector3d vec, double radius, double startAngle, double endAngle){ AcDbObjectId arcId; AcDbArc *pArc = new AcDbArc(ptCenter, vec, radius, startAngle, endAngle); arcId = CCreateEnt::PostToModelSpace(pArc); return arcId;}AcDbObjectId CCreateEnt::CreateArc(AcGePoint2d ptCenter, double radius, double startAngle, double endAngle){ AcGeVector3d vec(0, 0, 1); return CCreateEnt::CreateArc(CCalculation::Pt2dTo3d(ptCenter),vec, radius, startAngle, endAngle);}AcDbObjectId CCreateEnt::CreateArc(AcGePoint2d ptStart, AcGePoint2d ptOnArc, AcGePoint2d ptEnd){ // 使用几何类获得圆心、半径 AcGeCircArc2d geArc(ptStart, ptOnArc, ptEnd); AcGePoint2d ptCenter = geArc.center(); double radius = geArc.radius(); // 计算起始和终止角度 AcGeVector2d vecStart(ptStart.x - ptCenter.x, ptStart.y - ptCenter.y); AcGeVector2d vecEnd(ptEnd.x - ptCenter.x, ptEnd.y - ptCenter.y); double startAngle = vecStart.angle(); double endAngle = vecEnd.angle(); return CCreateEnt::CreateArc(ptCenter, radius, startAngle, endAngle);}AcDbObjectId CCreateEnt::CreateArcSCE(AcGePoint2d ptStart, AcGePoint2d ptCenter, AcGePoint2d ptEnd){ // 计算半径 double radius = ptCenter.distanceTo(ptStart); // 计算起、终点角度 AcGeVector2d vecStart(ptStart.x - ptCenter.x, ptStart.y - ptCenter.y); AcGeVector2d vecEnd(ptEnd.x - ptCenter.x, ptEnd.y - ptCenter.y); double startAngle = vecStart.angle(); double endAngle = vecEnd.angle(); // 创建圆弧 return CCreateEnt::CreateArc(ptCenter, radius, startAngle, endAngle);}AcDbObjectId CCreateEnt::CreateArc(AcGePoint2d ptStart, AcGePoint2d ptCenter, double angle){ // 计算半径 double radius = ptCenter.distanceTo(ptStart); // 计算起、终点角度 AcGeVector2d vecStart(ptStart.x - ptCenter.x, ptStart.y - ptCenter.y); double startAngle = vecStart.angle(); double endAngle = startAngle + angle; // 创建圆弧 return CCreateEnt::CreateArc(ptCenter, radius, startAngle, endAngle);}
最后,创建AutoCAD命令,达到在AutoCAD命令行中输入命令,布局窗口看到我们的大斧头的目的:
**********acrxEntryPoint.cpp*******************static void HHLCreateEntsLine(void) { AcGePoint3d ptStart(0, 100, 0); AcGePoint3d ptEnd(0, -100, 0); AcDbObjectId lineId; lineId = CCreateEnt::CreateLine(ptStart, ptEnd); CModifyEnt::ChangeColor(lineId, 1); ptStart.set(-10, 100, 0); ptEnd.set(-10, -100, 0); lineId = CCreateEnt::CreateLine(ptStart, ptEnd); CModifyEnt::ChangeColor(lineId, 1); CModifyEnt::ChangeLayer(lineId, _T("虚线")); CModifyEnt::ChangeLinetype(lineId, _T("中心线")); } static void HHLCreateEntsCircle(void) { // “圆心、半径”法创建一个圆 AcGePoint3d ptCenter(50, 50, 0); CCreateEnt::CreateCircle(ptCenter, 5); // 两点法创建一个圆 AcGePoint2d pt1(70, 100); AcGePoint2d pt2(130, 100); //CCreateEnt::CreateCircle(pt1, pt2); // 三点法创建一个圆 pt1.set(60, 100); pt2.set(140, 100); AcGePoint2d pt3(100, 60); //CCreateEnt::CreateCircle(pt1, pt2, pt3); } static void HHLCreateEntsArc(void) { // 创建位于XOY平面上的圆弧 AcGePoint2d ptCenter(50, -50); CCreateEnt::CreateArc(ptCenter, 100 * sqrt( (double)2 ) / 2, 1 * CCalculation::PI() / 4, 3 * CCalculation::PI() / 4); // 三点法创建圆弧 AcGePoint2d ptStart(100, 0); AcGePoint2d ptOnArc(120, 50); AcGePoint2d ptEnd(100, 100); CCreateEnt::CreateArc(ptStart, ptOnArc, ptEnd); // “起点、圆心、终点”创建圆弧 ptStart.set(100, 100); ptCenter.set(50, 150); ptEnd.set(0, 100); CCreateEnt::CreateArcSCE(ptEnd , ptCenter, ptStart); // “起点、圆心、圆弧角度”创建圆弧 ptStart.set(0, 0); ptCenter.set(-50, 50); CCreateEnt::CreateArc(ptStart, ptCenter, CCalculation::PI() / 2); }ACED_ARXCOMMAND_ENTRY_AUTO(CCreateEntsApp, HHL, CreateEntsLine, CreateLine, ACRX_CMD_TRANSPARENT, NULL)ACED_ARXCOMMAND_ENTRY_AUTO(CCreateEntsApp, HHL, CreateEntsCircle, CreateCircle, ACRX_CMD_TRANSPARENT, NULL)ACED_ARXCOMMAND_ENTRY_AUTO(CCreateEntsApp, HHL, CreateEntsArc, CreateArc, ACRX_CMD_TRANSPARENT, NULL)
注意:最后这部分代码是加在现有cpp里的,该cpp为程序的主入口