private Point3d GetArcCenter(Point3d pt1,Point3d pt2,Point3d pt3,ref double radius)
{
double xysm, xyse, xy;
double[] m_ArcCenter = new double[3];
xy = Math.Pow(pt1.X, 2) + Math.Pow(pt1.Y, 2);
xyse = xy - Math.Pow(pt3.X, 2) - Math.Pow(pt3.Y, 2);
xysm = xy - Math.Pow(pt2.X, 2) - Math.Pow(pt2.Y, 2);
xy = (pt1.X - pt2.X) * (pt1.Y - pt3.Y) - (pt1.X - pt3.X) * (pt1.Y - pt2.Y);
// 判断参数有效性
if (Math.Abs(xy)<0.000001)
{
MessageBox.Show("所输入的参数无法创建圆形!");
return Point3d.Origin;
}
// 获得圆心和半径
m_ArcCenter[0] = ((xysm * (pt1.Y - pt3.Y)) - (xyse * (pt1.Y - pt2.Y))) / (2 * xy);
m_ArcCenter[1] = ((xyse * (pt1.X - pt2.X)) - (xysm * (pt1.X - pt3.X))) / (2 * xy);
m_ArcCenter[2] = 0;
radius = Math.Sqrt((pt1.X - m_ArcCenter[0]) * (pt1.X - m_ArcCenter[0]) + (pt1.Y - m_ArcCenter[1]) * (pt1.Y - m_ArcCenter[1]));
if (radius<0.000001)
{
MessageBox.Show("半径过小");
return Point3d.Origin;
}
// 函数返回圆心的位置,而半径在参数中通过引用方式返回
Point3d arcCenter = new Point3d(m_ArcCenter[0], m_ArcCenter[1], m_ArcCenter[2]);
return arcCenter;
}
public Arc AddArcFromPt(Point3d ptSt,Point3d ptSc,Point3d ptEn,ObjectId objid,string featurename)
{
// 三点法创建圆弧
Point3d ptCenter;
double radius = 0.0;
Arc objArc;
ptCenter = GetArcCenter(ptSt, ptSc, ptEn, ref radius);
if (isClockWise(ptCenter, ptSt, ptSc, ptEn))
objArc = AddArcCSEP(ptCenter, ptSt, ptEn, radius,objid,featurename);
else
objArc = AddArcCSEP(ptCenter, ptEn, ptSt, radius,objid,featurename);
return objArc;
}
private Arc AddArcCSEP(Point3d ptCenter,Point3d ptSt,Point3d ptEn,double radius,ObjectId objid,string featurename)
{
double radiusent;
double stAng, enAng;
// 计算半径
radiusent = radius;
// 计算起点角度和终点角度
stAng = CalLineAngle(ptCenter, ptSt);
enAng = CalLineAngle(ptCenter, ptEn);
Arc objArc = new Arc(ptCenter, radius, stAng, enAng);
objArc.LinetypeId = objid;
objArc.Layer = featurename;
objArc.SetDatabaseDefaults();
return objArc;
}
private bool isClockWise(Point3d ptCenter,Point3d ptSt,Point3d ptSc,Point3d ptEn)
{
double a1, a2, a3;
a1 = CalLineAngle(ptSt, ptCenter);
a2 = CalLineAngle(ptSc, ptCenter);
a3 = CalLineAngle(ptEn, ptCenter);
bool isClock = (a1 < a2) | (a2 < a3) | (a1 < a3);
return isClock;
}
private double CalLineAngle(Point3d vStartPt,Point3d vEndPt)
{
double X, Y;
double CalculateLineAngle = 0.0;
double dbJudgeAngle = 0.001;
X = vEndPt.X - vStartPt.X;
Y = vEndPt.Y - vStartPt.Y;
if ((X > 0 && Y > 0)||(X > 0 && Y < 0))
{
CalculateLineAngle = Math.Atan(Y / X);
}
else if ((X < 0 && Y > 0) || (X < 0 && Y < 0))
{
CalculateLineAngle = Math.PI + Math.Atan(Y / X);
}
else if(X == 0)
{
if (Y < 0)
{
CalculateLineAngle = -Math.PI / 2;
}
else if (Y > 0)
{
CalculateLineAngle = Math.PI / 2;
}
}
else if (Y == 0)
{
if (X < 0)
{
CalculateLineAngle = Math.PI;
}
else if (X > 0)
{
CalculateLineAngle = 0;
}
}
else
{
;
}
if (Math.Abs(CalculateLineAngle - 0) < dbJudgeAngle)
{
CalculateLineAngle = 0;
return CalculateLineAngle;
}
else if (CalculateLineAngle < 0)
{
CalculateLineAngle = 2 * Math.PI + CalculateLineAngle;
return CalculateLineAngle;
}
else
return CalculateLineAngle;
}