ObjectARX编程--圆

最近在学习ObjectARX,于是想着把一些东西写到这里供个人和大家参考,博客中出现的问题我会尽快改正,全篇代码基于《[c++]ObjectARX开发实例教程》进行整理与扩展,至于能够做到哪一步,emmmm…
开发环境:VS2010 + ObjectARX2014 + AutoCAD2014 。环境搭建不再赘述,以下项目全部使用Objectarx向导进行创建,且全部放在一个项目当中,如果出现相同部分则不再重复。部分非重要代码可能在之前的博客中出现过,这里就不再出现,需要的可以翻之前的。注意:所有成员函数记得在头文件进行声明,以下不再进行赘述

创建圆

以下分别通过“圆心,半径”,“直径和两端点”和“三点法”创建圆
CreateEnt.h:

static AcDbObjectId CreateCircle(AcGePoint3d ptCenter,AcGeVector3d vec, double radius);//画圆
static AcDbObjectId CreateCircle(AcGePoint3d ptCenter, double radius);
static AcDbObjectId CreateCircle(AcGePoint2d pt1, AcGePoint2d pt2);//两点画圆
static AcDbObjectId CreateCircle(AcGePoint2d pt1, AcGePoint2d pt2,AcGePoint2d pt3);//三点画圆

CreateEnt.cpp:

//圆心,半径
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;
	//创建圆
	return CCreateEnt::CreateCircle(ptCenter,radius)
}

//三点画圆
AcDbObjectId CCreateEnt::CreateCircle(AcGePoint2d pt1,AcGePoint2d pt2, AcGePoint2d pt3){
	//使用几何类
	AcGeCircArc2d geArc(pt1,pt2,pt3);
	AcGePoint3d ptCenter(geArc.center().x,geArc.center().y,0);
	return CCreateEnt::CreateCircle(ptCenter,geArc.radius());
}
  • 在项目中添加一个新类 CCalculation,用于封装计算的相关函数,添加两个重载静态函数 MiddlePoint,用于计算两点连线的中点:
//获得两点连线的中点,也就是圆心
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; 
} 

#include "gearc3d.h" 
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; 
} 
  • 在项目中注册一个新命令 AddCircle,对几个创建圆的函数进行测试
//acrxEntryPoint.cpp:
static void MLMyGroupAddCircle() 
{ 
	// “圆心、半径”法创建一个圆
	AcGePoint3d ptCenter(100, 100, 0); 
	CCreateEnt::CreateCircle(ptCenter, 20); 
	// 两点法创建一个圆
	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);  
}

ACED_ARXCOMMAND_ENTRY_AUTO(CArxProjectMLApp, MLMyGroup, AddCircle, CreateCircler, ACRX_CMD_MODAL, NULL)//画圆

实现效果

ObjectARX编程--圆_第1张图片

你可能感兴趣的:(ObjectARX)