AutoCAD_建立直线,圆弧,圆

首先看一下效果图:数据库

  

分为4部分,先看代码最少的计算模块:app

**********Calculation.h*******************

#pragma once

class 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"
#include <math.h>

CCalculation::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 <math.h>
#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为程序的主入口3d

相关文章
相关标签/搜索