- UID
- 177683
- 积分
- 0
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2004-9-23
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
#region Using directives
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using DBTransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
#endregion
namespace ZHFArxLibrary
{
public class Arx
{
private Database db;
private DBTransMan tm;
private Transaction myT;
BlockTable bt;
BlockTableRecord btr;
public Arx()
{
db = HostApplicationServices.WorkingDatabase;
tm = db.TransactionManager;
}
#region Line
public Line AddLine(Point3d pt1, Point3d pt2)
{
Line line;
try
{
Initialize();
line = new Line(pt1, pt2);
AddEntity(line);
}
finally
{
DisposeAll();
}
return line;
}
public Line AddLine(Point2d pt1, Point2d pt2)
{
Line line;
line = AddLine(new Point3d(pt1.X, pt1.Y, 0), new Point3d(pt2.X, pt2.Y, 0));
return line;
}
public Line AddLine(Point3d pt1, double x, double y)
{
Line line;
Point3d pt2 = new Point3d();
pt2.X = pt1.X + x;
pt2.Y = pt1.Y + y;
pt2.Z = pt1.Z;
line = AddLine(pt1, pt2);
return line;
}
public Line AddLineR(Point3d pt1, double angle, double length)
{
Line line;
Point3d pt2 = new Point3d();
pt2 = GetPointAR(pt1, angle, length);
line = AddLine(pt1, pt2);
return line;
}
#endregion
#region Polyline
public Polyline AddLWPolyline(Point2dCollection ptArr, double width)
{
Polyline pline = new Polyline();
try
{
Initialize();
for (int i = 0; i < ptArr.Count; i++)
{
pline.AddVertexAt(i, ptArr, 0, width, width);
}
AddEntity(pline);
}
finally
{
DisposeAll();
}
return pline;
}
public Polyline AddLWPolyline(Point2d ptStart, Point2d ptEnd, double width)
{
Polyline pline;
Point2dCollection ptArr = new Point2dCollection(); ;
ptArr.Add(ptStart);
ptArr.Add(ptEnd);
pline = AddLWPolyline(ptArr, width);
return pline;
}
public Polyline3d AddPolyline(Point3dCollection ptArr)
{
Polyline3d pline;
try
{
Initialize();
pline = new Polyline3d(Poly3dType.SimplePoly, ptArr, false);
AddEntity(pline);
}
finally
{
DisposeAll();
}
return pline;
}
public Polyline3d AddPolyline(Point3d pt1, Point3d pt2)
{
Polyline3d pline;
Point3dCollection ptArr = new Point3dCollection();
ptArr.Add(pt1);
ptArr.Add(pt2);
pline = AddPolyline(ptArr);
return pline;
}
public Polyline AddRectangle(Point2d pt1, Point2d pt2, double width)
{
Polyline pline;
Point2dCollection ptArr = new Point2dCollection();
Point2d pt3 = new Point2d(pt2.X,pt1.Y);
Point2d pt4 = new Point2d(pt1.X, pt2.Y);
ptArr.Add(pt1);
ptArr.Add(pt3);
ptArr.Add(pt2);
ptArr.Add(pt4);
ptArr.Add(pt1);
pline = AddLWPolyline(ptArr, width);
return pline;
}
public Polyline AddRectangle(Point2d pt1, Point2d pt2)
{
return AddRectangle(pt1, pt2, 0);
}
public Polyline AddPolygon(Point2d center, int number, double radius, double width, double angle)
{
Polyline pline;
Point2dCollection ptArr = new Point2dCollection();
double ang;
ang = 2 * Math.PI / number;
Point2d pt2d = new Point2d();
for (int i = 0; i <=number; i++)
{
pt2d.X = center.X + radius * Math.Cos(i * ang);
pt2d.Y = center.Y + radius * Math.Sin(i * ang);
ptArr.Add(pt2d);
}
pline = AddLWPolyline(ptArr, width);
return pline;
}
public Polyline AddPolygon(Point2d center, int number, double radius)
{
return AddPolygon(center, number, radius, 0, 0);
}
public Polyline AddPolygon(Point2d center, int number, double radius, double width)
{
return AddPolygon(center, number, radius, width, 0);
}
#endregion
public void Initialize()
{
myT = tm.StartTransaction();
bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead, false);
btr = (BlockTableRecord)tm.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false);
}
public void DisposeAll()
{
bt.Close();
btr.Close();
myT.Dispose();
}
public Point3d GetPointAR(Point3d pt1, double angle, double length)
{
Point3d pt2 = new Point3d();
pt2.X = pt1.X + length * Math.Cos(angle);
pt2.Y = pt1.Y + length * Math.Sin(angle);
pt2.Z = pt1.Z;
return pt2;
}
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;
}
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;
}
public double GetReal(string prompt)
{
double d = ((AcadApplication)Application.AcadApplication).ActiveDocument.Utility.GetReal(prompt);
return d;
}
public double[] Point3dToObject(Point3d pt)
{
double[] d = new double[3];
d[0] = pt.X;
d[1] = pt.Y;
d[2] = pt.Z;
return d;
}
public double AngleFromAxis(Point3d pt1, Point3d pt2)
{
double angle = ((AcadApplication)Application.AcadApplication).ActiveDocument.Utility.AngleFromXAxis(Point3dToObject(pt1), Point3dToObject(pt2));
return angle;
}
public Point3d GetCenterOf3Pt(Point3d pt1, Point3d pt2, Point3d pt3, out double radius)
{
double xysm, xyse, xy;
Point3d center;
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所输入的参数无法创建图形!");
radius = 0;
Environment.Exit(0);
}
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("半径过小!");
Environment.Exit(0);
}
}
return center;
}
public Point3d GetMidPoint(Point3d pt1, Point3d pt2)
{
Point3d pt = new Point3d();
pt.X = (pt1.X + pt2.X) / 2;
pt.Y = (pt1.Y + pt2.Y) / 2;
pt.Z = 0;
return pt;
}
public void AddEntity(Entity entity)
{
btr.AppendEntity(entity);
tm.AddNewlyCreatedDBObject(entity, true);
myT.Commit();
}
}
} |
|