ObjectARX编程--圆弧

创建圆弧

CCalculation.cpp

AcGePoint3d CCalculation::Pt2dTo3d(AcGePoint2d pt)
{ 
    AcGePoint3d ptTemp(pt.x,pt.y,0);
    return ptTemp;
}
#include 
double CCalculation::PI()
{
    return* atan(1.0);
}

CreateEnt.cpp

//创建圆弧
AcDbObjectId CCreateEnt::CreateArc(AcGePoint3d ptCenter, AcGeVector3d vec, double radius, double starAngle, double endAngle)
{ 
    AcDbArc *pArc = new AcDbArc(ptCenter, vec, radius, startAngle, endAngle);
    //(圆心,平面,半径,起点角度,终点角度)
    /*StartAngle和EndAngle并不是起终点的角度,StartAngle可以认为是个参考角,
    默认是0,如果将圆弧绕圆心旋转10度,那这个StartAngle就是10度,EndAngle始终不变,是圆弧的圆心角。*/
    AcDbObjectId arcId;
    arcId = CCreate::PostToModelSpace(pArc); 
    return arcId;
}

//创建位于XOY平面上的圆弧的函数
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();	
    /*AcGeVector2d 类用来表示一个二维空间中的矢量,
    其成员函数 angle 返回该矢量和 X 轴正半轴的角度(用弧度来表示)。*/
    return CCreateEnt::CreateArc(ptCenter, radius, startAngle, endAngle);
}

//"起点,圆心,终点"创建圆弧
//这个函数的名称不再是 CreateArc,而是 CreateArcSCE,这是因为该函数的参数列表、返回值都与三点法的函数相同,无法实现函数的重载,就只能重新定义一个新的函数名称。
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);
}

acrxEntryPoint.cpp

#include "Calculation.h"
static void MLMyGroupAddArc()
{
    // 创建位于XOY平面上的圆弧-
    AcGePoint2d ptCenter(50, 50); 
    CCreateEnt::CreateArc(ptCenter, 100 * sqrt((double)2) / 2, 5 * CCalculation::PI() / 4, 7 * 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, 50); 
    ptEnd.set(0, 100); 
    CCreateEnt::CreateArcSCE(ptStart, ptCenter, ptEnd); 
  
    // “起点、圆心、圆弧角度”创建圆弧
    ptStart.set(0, 100); 
    ptCenter.set(50, 50); 
    CCreateEnt::CreateArc(ptStart, ptCenter, CCalculation::PI() / 2); 
}

实现效果

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

你可能感兴趣的:(ObjectARX)