利用AutoCAD 2005.NET API进行AutoCAD的二次开发(三)
--圆
大家好!今天讲的内容是圆。对于在程序中加入相应的命名空间和一些前期的准备工作,请大家参考“利用AutoCAD 2005.NET API进行AutoCAD的二次开发(一)”。不要再另外创建新的工程,直接在“利用AutoCAD 2005.NET API进行AutoCAD的二次开发(一)” 创建的工程中加入相应的函数,这样可以创建一个自己的底层函数库。下面是编程的具体步骤:
(1) 创建圆的基准函数(AddCircle)。其实现的步骤如下:
public Circle AddCircle(Point3d center, double radius)
{
Circle circle;
try
{
Initialize();
circle = new Circle();
circle.Center = center;
circle.Radius = radius;
AddEntity(circle);
}
finally
{
DisposeAll();
}
return circle;
}
其中,Initialize、AddEntity 、DisposeAll是为了简化编程工作而自定义的函数,请大家参考“利用AutoCAD 2005.NET API进行AutoCAD的二次开发(一)”。
(2) 已知圆心和直径创建圆。
public Circle AddCircleD(Point3d center, double diameter)
{
Circle circle;
circle = AddCircle(center, diameter / 2.0);
return circle;
}
(3) 两点法创建圆。
public Circle AddCircle(Point3d pt1, Point3d pt2)
{
Circle circle;
Point3d center;
double diameter;
//获取圆心位置
center.X = (pt1.X + pt2.X) / 2;
center.Y = (pt1.Y + pt2.Y) / 2;
center.Z = (pt1.Z + pt2.Z) / 2;
diameter = Math.Sqrt((pt2.X - pt1.X) * (pt2.X - pt1.X) + (pt2.Y - pt1.Y) * (pt2.Y - pt1.Y));//获取直径
circle = AddCircleD(center, diameter);
return circle;
}
(4)三点法创建圆。关于三点法创建圆的理论请参考明经通道出版的《AutoCAD VBA 开发精彩实例教程》。
public Circle AddCircle(Point3d pt1, Point3d pt2, Point3d pt3)
{
double xysm, xyse, xy;
Point3d center;
double radius;
Circle circle;
xy = pt1.X * pt1.X + pt1.Y * pt1.Y;
xyse = xy - pt3.X * pt3.X - pt3.Y * pt3.Y;
xysm = xy - pt2.X * pt2.X - pt2.Y * pt2.Y;
xy = (pt1.X - pt2.X) * (pt1.Y - pt3.Y) - (pt1.X - pt3.X) * (pt1.Y - pt2.Y);
//判断参数有效性
if (Math.Abs(xy) < 0.000001)
{
CommandLinePrompts.Message("\n所输入的参数无法创建图形!");
circle = null;
}
else
{
//获取圆心
center.X = (xysm * (pt1.Y - pt3.Y) - xyse * (pt1.Y - pt2.Y)) / (2 * xy);
center.Y = (xyse * (pt1.X - pt2.X) - xysm * (pt1.X - pt3.X)) / (2 * xy);
center.Z = 0;
radius = Math.Sqrt((pt1.X - center.X) * (pt1.X - center.X) + ((pt1.Y - center.Y) * (pt1.Y - center.Y)));//获取半径
if (radius < 0.000001)
{
CommandLinePrompts.Message("半径过小!");
circle = null;
}
else
{
circle = AddCircle(center, radius);
}
}
return circle;
}
(5)添加测试代码,请创建一新的类库工程,然后加入以下代码:
#region Using directives
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using DBTransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager;
using ZHFArxLibrary;
#endregion
namespace ClassLibrary1
{
public class Class1
{
public Class1()
{
}
[CommandMethod("AddCircle")]
public static void AddCircle()
{
Arx arx = new Arx();
Point3d pt1, pt2, pt3;
double radius;
pt1 = arx.GetPoint("\n指定圆心:");//自定义函数函数接受用户输入
radius = arx.GetReal("\n输入半径:");//自定义函数函数接受用户输入
arx.AddCircle(pt1, radius);
pt1 = arx.GetPoint("\n指定圆心:");
radius = arx.GetReal("\n输入直径:");
arx.AddCircleD(pt1, radius);
pt1 = arx.GetPoint("\n输入第一点:");
pt2 = arx.GetPoint(pt1, "\n输入第二点:");//自定义函数函数接受用户输入
arx.AddCircle(pt1, pt2);
pt1 = arx.GetPoint("\n输入第一点:");
pt2 = arx.GetPoint(pt1, "\n输入第二点:");
pt3 = arx.GetPoint(pt2, "\n输入第三点:");
arx.AddCircle(pt1, pt2, pt3);//三点法创建圆
}
}
}
由于AutoCAD 2005.NET API没有接受用户输入的相关函数,所以只能通过自定义函数来解决。请在创建底层函数库的工程(就是创建直线、圆等的那个工程)中加入以下自定义函数:
//在AutoCAD中获取用鼠标点取的点,输入参数是参照点pt,命令行提示prompt
public Point3d GetPoint(Point3d pt, string prompt)
{
double[] d = new double[3];
d[0] = pt.X;
d[1] = pt.Y;
d[2] = pt.Z;
Array a = (Array)((AcadApplication)Application.AcadApplication).ActiveDocument.Utility.GetPoint(d, prompt);
Point3d point = new Point3d();
point.X = (double)a.GetValue(0);
point.Y = (double)a.GetValue(1);
point.Z = (double)a.GetValue(2);
return point;
}
//在AutoCAD中获取用鼠标点取的点,输入参数是命令行提示prompt
public Point3d GetPoint(string prompt)
{
Array a = (Array)((AcadApplication)Application.AcadApplication).ActiveDocument.Utility.GetPoint(Type.Missing, prompt);
Point3d point = new Point3d();
point.X = (double)a.GetValue(0);
point.Y = (double)a.GetValue(1);
point.Z = (double)a.GetValue(2);
return point;
}
//在AutoCAD中获取用户在命令行中输入的实数,输入参数是命令行提示prompt
public double GetReal(string prompt)
{
double d = ((AcadApplication)Application.AcadApplication).ActiveDocument.Utility.GetReal(prompt);
return d;
}
把(5)中的工程编译。然后启动AutoCAD,在命令行中键入netload命令,在弹出的对话框中选择刚编译好的dll文件。在命令行中键入AddCircle(就是你在(8)中声明的AutoCAD .NET命令),然后根据相应的命令行提示进行操作,可以在AutoCAD中用4种不同的方法画出4个圆。
(6)完整的程序代码请看下面的附件。
|