using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
namespace AcadTest
{
public class EllipseArcTest
{
public class EllipseTest
{
[CommandMethod("EllipticalArc3dTest")]
public void RunEllipticalArc3dTest()
{
Document adoc = Application.DocumentManager.MdiActiveDocument;
Editor ed = adoc.Editor;
Database db = adoc.Database;
PromptPointResult centerPtRes = ed.GetPoint("\nCenter: ");
if (centerPtRes.Status != PromptStatus.OK) return;
PromptPointResult leftPtRes = ed.GetPoint("\nLeft point on Ellipse: ");
if (leftPtRes.Status != PromptStatus.OK) return;
PromptPointResult rightPtRes = ed.GetPoint("\nRight point on Ellipse: ");
if (rightPtRes.Status != PromptStatus.OK) return;
PromptDoubleResult horRadRes = ed.GetDistance("\nHorizontal radius length: ");
if (horRadRes.Status != PromptStatus.OK) return;
PromptDoubleResult vertRadRes = ed.GetDistance("\nVertical radius length: ");
if (vertRadRes.Status != PromptStatus.OK) return;
EllipticalArc3d elArc3d = GetEllipseArc
(leftPtRes.Value, rightPtRes.Value,
centerPtRes.Value, horRadRes.Value,
vertRadRes.Value);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Ellipse ellipse = new Ellipse(elArc3d.Center, Vector3d.ZAxis,
elArc3d.MajorAxis * elArc3d.MajorRadius,
elArc3d.MinorRadius / elArc3d.MajorRadius,
elArc3d.StartAngle, elArc3d.EndAngle);
ellipse.ColorIndex = 1;
ellipse.LineWeight = LineWeight.LineWeight050;
BlockTableRecord space = tr.GetObject
(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
space.AppendEntity(ellipse);
tr.AddNewlyCreatedDBObject(ellipse, true);
double rad = ellipse.MinorRadius / 10;
CreateCircle(tr, space, leftPtRes.Value, rad, 1);
CreateCircle(tr, space, rightPtRes.Value, rad, 1);
CreateCircle(tr, space, elArc3d.StartPoint, rad, 5);
CreateCircle(tr, space, elArc3d.EndPoint, rad, 5);
tr.Commit();
}
ed.WriteMessage
("\nSelected points. Start: {0}, end: {1}",
leftPtRes.Value, rightPtRes.Value);
ed.WriteMessage
("\nPoints from arc. Start: {0}, end: {1}",
elArc3d.StartPoint, elArc3d.EndPoint);
}
static void CreateCircle(Transaction tr,
BlockTableRecord space, Point3d center,
double radius, byte color)
{
Circle circle = new Circle(center, Vector3d.ZAxis, radius);
circle.ColorIndex = color;
circle.LineWeight = LineWeight.LineWeight050;
space.AppendEntity(circle);
tr.AddNewlyCreatedDBObject(circle, true);
}
static EllipticalArc3d GetEllipseArc
(Point3d startPt, Point3d endPt,
Point3d centerPt, double horRadius,
double vertRadius)
{
double
firstAngle = new Vector2d
(startPt.X - centerPt.X,
startPt.Y - centerPt.Y).Angle,
secondAngle = new Vector2d
(endPt.X - centerPt.X,
endPt.Y - centerPt.Y).Angle;
if (centerPt.Y < startPt.Y)
{
double tmp = firstAngle;
firstAngle = secondAngle;
secondAngle = tmp;
}
Vector3d major, minor;
double majorRad, minorRad;
if (horRadius > vertRadius)
{
major = Vector3d.XAxis * horRadius;
minor = Vector3d.YAxis * vertRadius;
majorRad = horRadius;
minorRad = vertRadius;
}
else
{
major = Vector3d.YAxis * vertRadius;
minor = Vector3d.XAxis * horRadius;
majorRad = vertRadius;
minorRad = horRadius;
firstAngle -= Math.PI / 2;
secondAngle -= Math.PI / 2;
}
return new EllipticalArc3d(centerPt,
major, minor, majorRad, minorRad, firstAngle, secondAngle);
}
}
}
}