c++ Arx二次开发创建椭圆和样条曲线
一、本节课程c++ Arx二次开发创建椭圆和样条曲线
二、本节要讲解的知识点
1、如何应用C++ ARX二次开发创建椭圆(对AcDbEllipse类的构造函数的直接封装和根据外接矩形来创建椭圆)
2、如何应用C++ ARX二次开发创建样条曲线(对AcDbSpline类的构造函数的直接封装)
三、具体内容
1、创建椭圆的思路
有参数构造函数:AcDbEllipse(
const AcGePoint3d& center,
const AcGeVector3d& unitNormal,
const AcGeVector3d& majorAxis,
double radiusRatio,
double startAngle = 0.0,
double endAngle = 6.28318530717958647692
);
(1)AcDbEllipse类的构造函数,给定中心点、所在平面、长轴的一个端点和半径比例来创建椭圆。半径比例是一个用来定义椭圆的短轴相对于长轴的比例的参数。半径比例为1时,椭圆就变为圆。startAngle、endAngle这两个参数的配合使用,可以创建部分椭圆(椭圆弧)。
(2)根据外接矩形来创建椭圆,椭圆的长、短轴的端点就是外接矩形的四条边的中点。因此,只要外接矩形的角点确定,椭圆的大小和形状就能计算出来。
2、创建椭圆的步骤和代码实现:
AcDbObjectId CEllipseUtil::Add(const AcGePoint3d &ptCenter,const AcGeVector3d & vecNormal,const AcGeVector3d &majorAxis,double ratio)
{
AcDbEllipse *pEllipse=new AcDbEllipse(ptCenter,vecNormal,majorAxis,ratio);
return CDwgDatabaseUtil::PostToModelSpace(pEllipse);//将对象添加到模型空间的函数
}
AcDbObjectId CEllipseUtil::Add(const AcGePoint2d &pt1,const AcGePoint2d &pt2)//pt1,pt2位矩形的两个角
{
AcGePoint3d ptCenter;
ptCenter=CGePointUtil::GetMiddlePoint(CConvertUtil::ToPoint3d(pt1),CConvertUtil::ToPoint3d(pt2));
AcGeVector3d vecNormal(0,0,1);
AcGeVector3d majorAxis(fabs(pt1.x-pt2.x)/2,0,0);
double ratio=fabs((pt1.y-pt2.y)/(pt1.x-pt2.x));
return Add(ptCenter,vecNormal,majorAxis,ratio);
}
3、创建样条曲线的思路
样条曲线在地形图的等高线经常被定义为样条曲线,创建样条曲线我们是对AcDbSpline类构造函数的封装。
AcDbSpline(
const AcGePoint3dArray& fitPoints,
int order = 4,
double fitTolerance = 0.0
);
int order = 4
Order of the spline to be created (in the range 2 to 26)
double fitTolerance = 0.0
Tolerance to which the spline should approximate fitPoints
points
Array of points (in WCS coordinates) through which to fit the curve
4、创建样条曲线的步骤和代码实现
AcDbObjectId CSplineUtil::Add(const AcGePoint3dArray &points,int order,double fitTolerance)
{
assert(order>=2&& order<=26);
AcDbSpline *pSpline=new AcDbSpline(points,order,fitTolerance);
AcDbObjectId splineId=CDwgDatabaseUtil::PostToModelSpace(pSpline);
return splineId;
}
再添加一个创建样条曲线的函数,相比上述创建样条曲线的函数多了两个参数,分别用于指定样条曲线的起点和终点的切向方向。
AcDbObjectId CSplineUtil::Add(const AcGePoint3dArray &points,const AcGeVector3d startTangent,const AcGeVector3d endTangent, int order,double fitTolerance)
{
assert(order>=2&& order<=26);
AcDbSpline *pSpline=new AcDbSpline(points,startTangent,endTangent,order,fitTolerance);
AcDbObjectId splineId=CDwgDatabaseUtil::PostToModelSpace(pSpline);
return splineId;
}
5、在acrxEntryPoint里面实现我们的命令(添加椭圆命令):
命令的定义:ACED_ARXCOMMAND_ENTRY_AUTO(CArxProject1App, YunyouMyGroup,AddEllipse, AddEllipse, ACRX_CMD_MODAL, NULL)
static void YunyouMyGroupAddEllipse()
{
//使用中心点、所在的平面、长轴矢量和短长轴比例来创建椭圆
AcGeVector3d vecNormal(0,0,1);
AcGeVector3d majorAxis(40,0,0);
AcDbObjectId entId;
entId=CEllipseUtil::Add(AcGePoint3d::kOrigin,vecNormal,majorAxis,0.5);
//指定外接矩形来创建椭圆
AcGePoint2d pt1(60,80),pt2(140,120);
CEllipseUtil::Add(pt1,pt2);
}
6、在acrxEntryPoint里面实现我们的命令(添加样条曲线的命令):
命令的定义:ACED_ARXCOMMAND_ENTRY_AUTO(CArxProject1App, YunyouMyGroup,AddSpline, AddSpline, ACRX_CMD_MODAL, NULL)
static void YunyouMyGroupAddSpline()
{
//使用样本点直接创建样条曲线
AcGePoint3d pt1(0,0,0),pt2(10,30,0),pt3(60,80,0),pt4(100,100,0);
AcGePoint3dArray points;
points.append(pt1);
points.append(pt2);
points.append(pt3);
points.append(pt4);
CSplineUtil::Add(points);
//指定起始点和终止点的切向方向来创建样条曲线
pt2.set(30,10,0);
pt3.set(80,60,0);
points.removeSubArray(0,3);
points.append(pt1);
points.append(pt2);
points.append(pt3);
points.append(pt4);
AcGeVector3d startTangent(5,1,0);
AcGeVector3d endTangent(5,1,0);
CSplineUtil::Add(points,startTangent,endTangent);
}
页:
[1]