006集—— 关于CAD二开的一些函数封装(CAD—C#二次开发入门)

241008版的封装


public static class GetTools
{
    public static string GetString(string message)
    {
        Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
        PromptStringOptions options = new PromptStringOptions(message);
        PromptResult @string = editor.GetString(options);
        return @string.StringResult;
    }

    public static bool GetPoint(out Point3d point, string message)
    {
        Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
        PromptPointOptions options = new PromptPointOptions(message);
        PromptPointResult point2 = editor.GetPoint(options);
        if (point2.Status == PromptStatus.OK)
        {
            point = point2.Value;
            return true;
        }

        editor.WriteMessage("\n用户取消了点的选择.");
        point = point2.Value;
        return false;
    }

    public static void Erase(this Database db, Entity entity)
    {
        using Transaction transaction = db.TransactionManager.StartTransaction();
        entity.ObjectId.GetObject(OpenMode.ForWrite);
        entity.Erase();
        transaction.Commit();
    }

    public static Entity GetEntity(this Database db, string message)
    {
        Entity result = null;
        Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
        PromptEntityResult entity = editor.GetEntity("");
        if (entity.Status == PromptStatus.OK)
        {
            using (db.TransactionManager.StartTransaction())
            {
                result = (Entity)entity.ObjectId.GetObject(OpenMode.ForRead);
            }
        }

        return result;
    }

    public static bool GetEntities(this Database db, out List<Curve> curves, string message)
    {
        Document mdiActiveDocument = Application.DocumentManager.MdiActiveDocument;
        Editor editor = mdiActiveDocument.Editor;
        List<Curve> list = new List<Curve>();
        PromptSelectionOptions promptSelectionOptions = new PromptSelectionOptions();
        promptSelectionOptions.AllowDuplicates = true;
        PromptSelectionResult selection = editor.GetSelection(promptSelectionOptions);
        if (selection.Status == PromptStatus.OK)
        {
            List<Curve> list2 = new List<Curve>();
            ObjectId[] objectIds = selection.Value.GetObjectIds();
            using (HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
            {
                ObjectId[] array = objectIds;
                foreach (ObjectId objectId in array)
                {
                    Entity entity = objectId.GetObject(OpenMode.ForRead) as Entity;
                    if (entity is Curve)
                    {
                        list2.Add(entity as Curve);
                    }
                }
            }

            curves = list2;
            int count = list2.Count;
            return count > 0;
        }

        curves = null;
        return false;
    }

    public static List<T> SelectEntities<T>(this Database db) where T : Entity
    {
        List<T> list = new List<T>();
        Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
        PromptSelectionResult selection = editor.GetSelection();
        if (selection.Status == PromptStatus.OK)
        {
            ObjectId[] objectIds = selection.Value.GetObjectIds();
            Database workingDatabase = HostApplicationServices.WorkingDatabase;
            using (workingDatabase.TransactionManager.StartTransaction())
            {
                ObjectId[] array = objectIds;
                foreach (ObjectId objectId in array)
                {
                    Entity entity = objectId.GetObject(OpenMode.ForRead) as Entity;
                    if (entity is T)
                    {
                        list.Add(entity as T);
                    }
                }
            }
        }

        return list;
    }

    public static ObjectId GetEntityInBlock(string message)
    {
        Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
        PromptNestedEntityOptions options = new PromptNestedEntityOptions(message);
        PromptNestedEntityResult nestedEntity = editor.GetNestedEntity(options);
        return nestedEntity.ObjectId;
    }

    public static List<Curve> ExplodePolyLine(Polyline polyline)
    {
        DBObjectCollection dBObjectCollection = new DBObjectCollection();
        polyline.Explode(dBObjectCollection);
        List<Curve> list = new List<Curve>();
        foreach (Curve item in dBObjectCollection)
        {
            list.Add(item);
        }

        return list;
    }

    public static Polyline CreatePolyLineByExtents3d(Extents3d extents)
    {
        Polyline polyline = new Polyline();
        polyline.AddVertexAt(0, new Point2d(extents.MinPoint.X, extents.MinPoint.Y), 0.0, 0.0, 0.0);
        polyline.AddVertexAt(1, new Point2d(extents.MaxPoint.X, extents.MinPoint.Y), 0.0, 0.0, 0.0);
        polyline.AddVertexAt(1, new Point2d(extents.MaxPoint.X, extents.MaxPoint.Y), 0.0, 0.0, 0.0);
        polyline.AddVertexAt(2, new Point2d(extents.MinPoint.X, extents.MaxPoint.Y), 0.0, 0.0, 0.0);
        polyline.Closed = true;
        return polyline;
    }

    public static Curve GetClosestCurve(this Database db, Point3d point, Vector3d vector, List<Curve> curves)
    {
        Ray ray = new Ray();
        ray.BasePoint = point;
        ray.UnitDir = vector;
        Point3d point3d = default(Point3d);
        double num = double.MaxValue;
        Curve result = null;
        foreach (Curve curf in curves)
        {
            Point3dCollection point3dCollection = new Point3dCollection();
            ray.IntersectWith(curf, Intersect.OnBothOperands, point3dCollection, IntPtr.Zero, IntPtr.Zero);
            foreach (Point3d item in point3dCollection)
            {
                double num2 = item.DistanceTo(point);
                if (num2 < num)
                {
                    point3d = item;
                    num = num2;
                    result = curf;
                }
            }
        }

        return result;
    }

    public static void GetCurve(this Database db)
    {
        LineSegment3d fromGeCurve = new LineSegment3d(default(Point3d), new Point3d(10.0, 10.0, 0.0));
        Line line = new Line(new Point3d(5.0, 5.0, 0.0), new Point3d(20.0, 0.0, 0.0));
        line.SetFromGeCurve(fromGeCurve);
    }

    public static ObjectId GetCenterPl(this Database db)
    {
        List<Polyline> list = db.SelectEntities<Polyline>();
        if (list.Count < 2)
        {
            return ObjectId.Null;
        }

        Polyline polyline = list[0];
        Polyline polyline2 = list[1];
        int num = 1000;
        double num2 = polyline.Length / (double)num;
        double num3 = polyline2.Length / (double)num;
        Polyline polyline3 = new Polyline();
        for (int i = 0; i < num; i++)
        {
            Point3d pointAtDist = polyline.GetPointAtDist(num2 * (double)i);
            Point3d pointAtDist2 = polyline2.GetPointAtDist(num3 * (double)i);
            LineSegment3d lineSegment3d = new LineSegment3d(pointAtDist, pointAtDist2);
            Point3d midPoint = lineSegment3d.MidPoint;
            if (polyline3.NumberOfVertices >= 2)
            {
                Vector3d vector3d = polyline3.GetPoint3dAt(polyline3.NumberOfVertices - 1) - polyline3.GetPoint3dAt(polyline3.NumberOfVertices - 2);
                Vector3d vector3d2 = midPoint - polyline3.GetPoint3dAt(polyline3.NumberOfVertices - 1);
                double angleTo = vector3d.GetAngleTo(Vector3d.XAxis, Vector3d.ZAxis);
                double angleTo2 = vector3d2.GetAngleTo(Vector3d.XAxis, Vector3d.ZAxis);
                if (Math.Abs(angleTo - angleTo2) < 0.001)
                {
                    polyline3.SetPointAt(polyline3.NumberOfVertices - 1, midPoint.Convert2d(new Plane()));
                    continue;
                }
            }

            polyline3.AddVertexAt(polyline3.NumberOfVertices, midPoint.Convert2d(new Plane()), 0.0, 0.0, 0.0);
        }

        return db.AddEntityToModeSpace(polyline3);
    }

    public static void Fangxiang()
    {
        Database workingDatabase = HostApplicationServices.WorkingDatabase;
        Entity entity = workingDatabase.GetEntity("");
        if (entity is Curve curve)
        {
            int num = 2;
            double num2 = curve.GetDistanceAtParameter(curve.EndParam) / (double)num;
            for (int i = 0; i < num; i++)
            {
                Point3d pointAtDist = curve.GetPointAtDist(num2 * (double)i);
                Vector3d vector3d = curve.GetFirstDerivative(pointAtDist).GetNormal() * num2;
                Vector3d vector3d2 = vector3d.RotateBy(2.617993877991494, Vector3d.ZAxis);
                Vector3d vector3d3 = vector3d.RotateBy(2.617993877991494, -Vector3d.ZAxis);
                Polyline polyline = new Polyline();
                polyline.ColorIndex = 2;
                polyline.AddVertexAt(0, (pointAtDist + vector3d2).Convert2d(new Plane()), 0.0, 0.0, 0.0);
                polyline.AddVertexAt(1, pointAtDist.Convert2d(new Plane()), 0.0, 0.0, 0.0);
                polyline.AddVertexAt(2, (pointAtDist + vector3d3).Convert2d(new Plane()), 0.0, 0.0, 0.0);
                workingDatabase.AddEntityToModeSpace(polyline);
                Application.DocumentManager.MdiActiveDocument.Editor.GetInteger("");
                workingDatabase.Erase(polyline);
            }
        }
    }
}

 

 public static partial class AddEntityTools
 {
     /// <summary>
     /// 将图形对象添加到图形文件中
     /// </summary>
     /// <param name="db">图形数据库</param>
     /// <param name="ent">图形对象</param>
     /// <returns>图形的ObjectId</returns>
     public static ObjectId AddEntityToModeSpace(this Database db, Entity ent)
     {
         // 声明ObjectId 用于返回
         ObjectId entId = ObjectId.Null;
         // 开启事务处理
         using (Transaction trans = db.TransactionManager.StartTransaction())
         {
             // 打开块表
             BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
             // 打开块表记录
             BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
             // 添加图形到块表记录
             entId = btr.AppendEntity(ent);
             // 更新数据信息
             trans.AddNewlyCreatedDBObject(ent, true);
             // 提交事务
             trans.Commit();
         }
         return entId;
     }


     /// <summary>
     /// 添加多个图形对象到图形文件中
     /// </summary>
     /// <param name="db">图形数据库</param>
     /// <param name="ent">图形对象 可变参数 传入一个数组</param>
     /// <returns>图形的ObjectId 数组返回</returns>
     public static ObjectId[] AddEntityToModeSpace(this Database db, params Entity[] ent)
     {
         // 声明ObjectId 用于返回
         ObjectId[] entId = new ObjectId[ent.Length];
         // 开启事务处理
         using (Transaction trans = db.TransactionManager.StartTransaction())
         {
             // 打开块表
             BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
             // 打开块表记录
             BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

             for (int i = 0; i < ent.Length; i++)
             {
                 // 将图形添加到块表记录
                 entId[i] = btr.AppendEntity(ent[i]);
                 // 更新数据信息
                 trans.AddNewlyCreatedDBObject(ent[i], true);

             }
             // 提交事务
             trans.Commit();
         }
         return entId;
     }


     /// <summary>
     /// 绘制直线
     /// </summary>
     /// <param name="db">图形数据库</param>
     /// <param name="startPoint">起点坐标</param>
     /// <param name="endPoint">终点坐标</param>
     /// <returns></returns>
     public static ObjectId AddLineToModeSpace(this Database db, Point3d startPoint, Point3d endPoint)
     {
         return db.AddEntityToModeSpace(new Line(startPoint, endPoint));
     }

     /// <summary>
     /// 起点坐标,角度,长度 绘制直线
     /// </summary>
     /// <param name="db">图形数据库</param>
     /// <param name="startPoint">起点</param>
     /// <param name="length">长苏</param>
     /// <param name="degree">角度</param>
     /// <returns></returns>
     public static ObjectId AddLineToModeSpace(this Database db, Point3d startPoint, Double length, Double degree)
     {
         // 利用长度和角度以及起点 计算终点坐标
         double X = startPoint.X + length * Math.Cos(degree.DegreeToAngle());
         double Y = startPoint.Y + length * Math.Sin(degree.DegreeToAngle());
         Point3d endPoint = new Point3d(X, Y, 0);
         return db.AddEntityToModeSpace(new Line(startPoint, endPoint));
     }



     // 封装圆弧对象函数
     /// <summary>
     /// 绘制圆弧
     /// </summary>
     /// <param name="db">图形数据库</param>
     /// <param name="center">中心</param>
     /// <param name="radius">半径</param>
     /// <param name="startDegree">起始角度</param>
     /// <param name="endDegree">终止角度</param>
     /// <returns></returns>

     public static ObjectId AddArcToModeSpace(this Database db, Point3d center, double radius, double startDegree, double endDegree)
     {
         return db.AddEntityToModeSpace(new Arc(center, radius, startDegree.DegreeToAngle(), endDegree.DegreeToAngle()));
     }


     /// <summary>
     /// 三点画圆弧
     /// </summary>
     /// <param name="db">数据库对象</param>
     /// <param name="startPoint">起点</param>
     /// <param name="pointOnArc">圆弧上一点</param>
     /// <param name="endPoint">终点</param>
     /// <returns></returns>
     public static ObjectId AddArcToModeSpace(this Database db, Point3d startPoint, Point3d pointOnArc, Point3d endPoint)
     {
         // 先判断是否在同一条直线上
         if (startPoint.IsOnOneLine(pointOnArc, endPoint))
         {
             return ObjectId.Null;
         }

         // 创建几何对象
         CircularArc3d cArc = new CircularArc3d(startPoint, pointOnArc, endPoint);

         // 通过几何对象获取其属性
         double radius = cArc.Radius; //半径

         /**************************************
         Point3d center = cArc.Center; // 所在圆的圆心
         Vector3d cs = center.GetVectorTo(startPoint); // 圆心到起点的向量
         Vector3d ce = center.GetVectorTo(endPoint); // 圆心到终点的向量
         Vector3d xVector = new Vector3d(1, 0, 0); // x正方向的向量
         // 圆弧起始角度
         double startAngle = cs.Y > 0 ? xVector.GetAngleTo(cs) : -xVector.GetAngleTo(cs);
         // 圆弧终止角度
         double endAngle = ce.Y > 0 ? xVector.GetAngleTo(ce) : -xVector.GetAngleTo(ce);
         ********************************************/

         // 创建圆弧对象
         Arc arc = new Arc(cArc.Center, cArc.Radius, cArc.Center.GetAngleToXAxis(startPoint), cArc.Center.GetAngleToXAxis(endPoint));
         // 加入到图形数据库
         return db.AddEntityToModeSpace(arc);
     }

     /// <summary>
     /// 绘制圆
     /// </summary>
     /// <param name="db">图形数据库</param>
     /// <param name="center">圆心</param>
     /// <param name="radius">半径</param>
     /// <returns></returns>
     public static ObjectId AddCircleModeSpace(this Database db, Point3d center, double radius)
     {
         return db.AddEntityToModeSpace(new Circle((center), new Vector3d(0, 0, 1), radius));
     }

     /// <summary>
     /// 两点绘制圆
     /// </summary>
     /// <param name="db">图形数据库</param>
     /// <param name="point1">第一个电</param>
     /// <param name="point2">第二个点</param>
     /// <returns></returns>
     public static ObjectId AddCircleModeSpace(this Database db, Point3d point1, Point3d point2)
     {
         // 获取两点的中心点
         Point3d center = point1.GetCenterPointBetweenTwoPoint(point2);
         // 获取半径
         double radius = point1.GetDistanceBetweenTwoPoint(center);
         return db.AddCircleModeSpace(center, radius);
     }

     /// <summary>
     /// 三点绘制圆
     /// </summary>
     /// <param name="db"></param>
     /// <param name="point1"></param>
     /// <param name="point2"></param>
     /// <param name="point3"></param>
     /// <returns></returns>
     public static ObjectId AddCircleModeSpace(this Database db, Point3d point1, Point3d point2, Point3d point3)

     {
         // 先判断三点是否在同一直线上
         if (point1.IsOnOneLine(point2, point3))
         {
             return ObjectId.Null;
         }
         // 声明几何类Circular3d对象
         CircularArc3d cArc = new CircularArc3d(point1, point2, point3);
         return db.AddCircleModeSpace(cArc.Center, cArc.Radius);
     }


     /// <summary>
     /// 绘制折线多段线
     /// </summary>
     /// <param name="db">图形数据库</param>
     /// <param name="isClosed">是否闭合</param>
     /// <param name="contantWidth">线宽</param>
     /// <param name="vertices">多线段的顶点 可变参数</param>
     /// <returns></returns>
     public static ObjectId AddPolyLineToModeSpace(this Database db, bool isClosed, double contantWidth, params Point2d[] vertices)
     {
         if (vertices.Length < 2)  // 顶点个数小于2 无法绘制
         {
             return ObjectId.Null;
         }
         // 声明一个多段线对象
         Polyline pline = new Polyline();
         // 添加多段线顶点
         for (int i = 0; i < vertices.Length; i++)
         {
             pline.AddVertexAt(i, vertices[i], 0, 0, 0);
         }
         if (isClosed)
         {
             pline.Closed = true;
         }
         // 设置多段线的线宽
         pline.ConstantWidth = contantWidth;
         return db.AddEntityToModeSpace(pline);
     }

     /// <summary>
     /// 绘制矩形
     /// </summary>
     /// <param name="db">图形数据库</param>
     /// <param name="point1">第一个点</param>
     /// <param name="point2">对角点</param>
     /// <returns></returns>
     public static ObjectId AddRectToModeSpace(this Database db, Point2d point1, Point2d point2)
     {
         // 声明多段线
         Polyline pLine = new Polyline();
         // 计算矩形的四个顶点坐标
         Point2d p1 = new Point2d(Math.Min(point1.X, point2.X), Math.Min(point1.Y, point2.Y));
         Point2d p2 = new Point2d(Math.Max(point1.X, point2.X), Math.Min(point1.Y, point2.Y));
         Point2d p3 = new Point2d(Math.Max(point1.X, point2.X), Math.Max(point1.Y, point2.Y));
         Point2d p4 = new Point2d(Math.Min(point1.X, point2.X), Math.Max(point1.Y, point2.Y));
         // 添加多段线顶点
         pLine.AddVertexAt(0, p1, 0, 0, 0); // 参数 索引值 传入点 多段线凸度 起始宽度 终止宽度
         pLine.AddVertexAt(0, p2, 0, 0, 0);
         pLine.AddVertexAt(0, p3, 0, 0, 0);
         pLine.AddVertexAt(0, p4, 0, 0, 0);
         pLine.Closed = true; // 闭合
         return db.AddEntityToModeSpace(pLine);
     }

     /// <summary>
     /// 绘制正多边形
     /// </summary>
     /// <param name="db">图形数据库</param>
     /// <param name="center">多边形所在内接圆圆心</param>
     /// <param name="radius">所在圆半径</param>
     /// <param name="sideNum">边数</param>
     /// <param name="startDegree">起始角度</param>
     /// <returns></returns>
     public static ObjectId AddPolygonToModeSpace(this Database db, Point2d center, double radius, int sideNum, double startDegree)
     {
         // 声明一个多段线对象
         Polyline pLine = new Polyline();
         // 判断边数是否符合要求
         if (sideNum < 3)
         {
             return ObjectId.Null;
         }
         Point2d[] point = new Point2d[sideNum]; // 有几条边就有几个点
         double angle = startDegree.DegreeToAngle();
         // 计算每个顶点坐标
         for (int i = 0; i < sideNum; i++)
         {
             point[i] = new Point2d(center.X + radius * Math.Cos(angle), center.Y + radius * Math.Sin(angle));
             pLine.AddVertexAt(i, point[i], 0, 0, 0);
             angle += Math.PI * 2 / sideNum;
         }
         // 闭合多段线
         pLine.Closed = true;
         return db.AddEntityToModeSpace(pLine);
     }
     /// <summary>
     /// 绘制椭圆
     /// </summary>
     /// <param name="db">图形数据库</param>
     /// <param name="center">椭圆中心</param>
     /// <param name="majorRadius">长轴长度</param>
     /// <param name="shortRadius">短轴长度</param>
     /// <param name="degree">长轴与X夹角 角度值</param>
     /// <param name="startDegree">起始角度</param>
     /// <param name="endDegree">终止角度</param>
     /// <returns></returns>
     public static ObjectId AddEllipseToModeSpace(this Database db, Point3d center, double majorRadius, double shortRadius, double degree, double startDegree, double endDegree)
     {
         // 计算相关参数
         double ratio = shortRadius / majorRadius;
         Vector3d majorAxis = new Vector3d(majorRadius * Math.Cos(degree.AngleToDegree()), majorRadius * Math.Sin(degree.DegreeToAngle()), 0);
         Ellipse elli = new Ellipse(center, Vector3d.ZAxis, majorAxis, ratio, startDegree.DegreeToAngle(), endDegree.DegreeToAngle()); // VVector3d.ZAxis 等价于 new Vector3d(0,0,1) 平行于z轴法向量
         return db.AddEntityToModeSpace(elli);
     }


     /// <summary>
     /// 三点绘制椭圆
     /// </summary>
     /// <param name="db">图形数据库</param>
     /// <param name="majorPoint1">长轴端点1</param>
     /// <param name="majorPoint2">长轴端点2</param>
     /// <param name="shortRadius">短轴的长度</param>
     /// <returns>ObjectId</returns>
     public static ObjectId AddEllipseToModeSpace(this Database db, Point3d majorPoint1, Point3d majorPoint2, double shortRadius)
     {
         // 椭圆圆心
         Point3d center = majorPoint1.GetCenterPointBetweenTwoPoint(majorPoint2);
         // 短轴与长轴的比例
         double ratio = 2 * shortRadius / majorPoint1.GetDistanceBetweenTwoPoint(majorPoint2);
         // 长轴的向量
         Vector3d majorAxis = majorPoint2.GetVectorTo(center);
         Ellipse elli = new Ellipse(center, Vector3d.ZAxis, majorAxis, ratio, 0, 2 * Math.PI);
         return db.AddEntityToModeSpace(elli);
     }

     /// <summary>
     /// 绘制椭圆 两点
     /// </summary>
     /// <param name="db"></param>
     /// <param name="point1">所在矩形的顶点</param>
     /// <param name="point2">所在矩形的顶点2</param>
     /// <returns></returns>
     public static ObjectId AddEllipseToModeSpace(this Database db, Point3d point1, Point3d point2)
     {
         // 椭圆圆心
         Point3d center = point1.GetCenterPointBetweenTwoPoint(point2);

         double ratio = Math.Abs((point1.Y - point2.Y) / (point1.X - point2.X));
         Vector3d majorVector = new Vector3d(Math.Abs((point1.X - point2.X)) / 2, 0, 0);
         // 声明椭圆对象
         Ellipse elli = new Ellipse(center, Vector3d.ZAxis, majorVector, ratio, 0, 2 * Math.PI);
         return db.AddEntityToModeSpace(elli);
     }
 }

 

using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AcTools
{
    public static partial class BaseTools
    {
        /// <summary>
        /// 角度转化为弧度
        /// </summary>
        /// <param name="degree">角度值</param>
        /// <returns></returns>
        public static double DegreeToAngle(this Double degree)
        {
            return degree * Math.PI / 180;
        }
        /// <summary>
        /// 弧度转换角度
        /// </summary>
        /// <param name="angle">弧度制</param>
        /// <returns></returns>
        public static double AngleToDegree(this Double angle)
        {
            return angle * 180 / Math.PI;
        }


        /// <summary>
        /// 判断三个点是否在同一直线上
        /// </summary>
        /// <param name="firstPoint">第一个点</param>
        /// <param name="secondPoint">第二个点</param>
        /// <param name="thirdPoint">第三个点</param>
        /// <returns></returns>
        public static bool IsOnOneLine(this Point3d firstPoint, Point3d secondPoint, Point3d thirdPoint)
        {
            Vector3d v21 = secondPoint.GetVectorTo(firstPoint);
            Vector3d v23 = secondPoint.GetVectorTo(thirdPoint);
            if (v21.GetAngleTo(v23) == 0 || v21.GetAngleTo(v23) == Math.PI)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static double GetAngleToXAxis(this Point3d startPoint, Point3d endPoint)
        {
            // 声明一个与X轴平行的向量
            Vector3d temp = new Vector3d(1, 0, 0);
            // 获取七点到终点的向量
            Vector3d VsToe = startPoint.GetVectorTo(endPoint);
            return VsToe.Y > 0 ? temp.GetAngleTo(VsToe) : -temp.GetAngleTo(VsToe);
        }

        public static double GetDistanceBetweenTwoPoint(this Point3d point1, Point3d point2)
        {
            return (Math.Sqrt(Math.Pow((point1.X - point2.X), 2) + Math.Pow((point1.Y - point2.Y), 2) + Math.Pow((point1.Z + point2.Z), 2)));
        }

        /// <summary>
        /// 获取两点的中心点
        /// </summary>
        /// <param name="point1">第一个点</param>
        /// <param name="point2">第二个点</param>
        /// <returns></returns>
        public static Point3d GetCenterPointBetweenTwoPoint(this Point3d point1, Point3d point2)
        {
            return new Point3d((point1.X + point2.X) / 2, (point1.Y + point2.Y) / 2, (point1.Z + point2.Z) / 2);
        }

    }
}

 

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AcTools
{
    public static partial class EditEntityTools
    {
        /// <summary>
        /// 改变图形颜色
        /// </summary>
        /// <param name="c1Id">图形的ObjectId</param>
        /// <param name="colorIndex">颜色索引</param>
        /// <returns>图形的ObjectId</returns> 图形已经添加图形数据库

        public static ObjectId ChangeEntityColor(this ObjectId c1Id, short colorIndex)
        {
            // 图形数据库
            Database db = HostApplicationServices.WorkingDatabase;
            // 开启事务处理
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                // 打开块表
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                // 打开块表记录
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                // 获取图形对象
                Entity ent1 = (Entity)c1Id.GetObject(OpenMode.ForWrite);
                // 设置颜色
                ent1.ColorIndex = colorIndex;
                // 提交事务处理
                trans.Commit();
            }
            return c1Id;
        }


        /// <summary>
        /// 改变图形颜色  图形没有添加到图形数据库
        /// </summary>
        /// <param name="ent">图形对象</param>
        /// <param name="colorIndex">颜色索引</param>
        /// <returns></returns>
        public static void ChangeEntityColor(this Entity ent, short colorIndex)
        {
            // 判断图形的IsNewlyObject
            if (ent.IsNewObject)
            {
                ent.ColorIndex = colorIndex;
            }
            // 不是新图形就调用上面的方法
            else
            {
                ent.ObjectId.ChangeEntityColor(colorIndex);
            }
        }



        /// <summary>
        ///  移动图形 图形已经加入到图形数据库中
        /// </summary>
        /// <param name="entId">图形对象的ObjectId</param>
        /// <param name="sourcePoint">参考原点</param>
        /// <param name="targetPoint">参考目标点</param>
        public static void MoveEntity(this ObjectId entId, Point3d sourcePoint, Point3d targetPoint)
        {
            // 打开当前图形数据库
            Database db = HostApplicationServices.WorkingDatabase;
            // 开启事务处理
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                // 打开块表
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                // 打开块表记录
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                //Entity ent = (Entity)trans.GetObject(entId, OpenMode.ForWrite);
                // 打开图形
                Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);
                // 计算变换矩阵
                Vector3d vectoc = sourcePoint.GetVectorTo(targetPoint);
                Matrix3d mt = Matrix3d.Displacement(vectoc);
                ent.TransformBy(mt);
                // 提交事务处理
                trans.Commit();
            }
        }


        /// <summary>
        ///  移动图形 图形没有加到图形数据库中
        /// </summary>
        /// <param name="entId">图形对象的ObjectId</param>
        /// <param name="sourcePoint">参考原点</param>
        /// <param name="targetPoint">参考目标点</param>
        public static void MoveEntity(this Entity ent, Point3d sourcePoint, Point3d targetPoint)
        {
            // 判断图形对象的IsNewlyObject属性
            if (ent.IsNewObject)
            {
                // 计算变换矩阵
                Vector3d vector = sourcePoint.GetVectorTo(targetPoint);
                Matrix3d mt = Matrix3d.Displacement(vector);
                ent.TransformBy(mt);
            }
            else
            {
                ent.ObjectId.MoveEntity(sourcePoint, targetPoint);
            }
        }



        /// <summary>
        ///  复制图形 图形已经加入到图形数据库中
        /// </summary>
        /// <param name="entId">图形对象的ObjectId</param>
        /// <param name="sourcePoint">参考原点</param>
        /// <param name="targetPoint">参考目标点</param>
        public static Entity CopyEntity(this ObjectId entId, Point3d sourcePoint, Point3d targetPoint)
        {
            // 声明一个图形对象
            Entity entR;
            // 当前图形数据库
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                // 打开块表
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                // 打开块表记录
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite );
                // Entity ent = (Entity)trans.GetObject(entId, OpenMode.ForWrite);
                // 打开图形
                Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);

                // 计算变换矩阵
                Vector3d vectoc = sourcePoint.GetVectorTo(targetPoint);
                Matrix3d mt = Matrix3d.Displacement(vectoc);
                entR = ent.GetTransformedCopy(mt);
                btr.AppendEntity(entR);
                trans.AddNewlyCreatedDBObject(entR, true);
                // 提交事务处理
                trans.Commit();
            }
            return entR;
        }



        /// <summary>
        ///  复制图形 图形没有加到图形数据库中
        /// </summary>
        /// <param name="ent">图形对象</param>
        /// <param name="sourcePoint">参考原点</param>
        /// <param name="targetPoint">参考目标点</param>
        public static Entity CopyEntity(this Entity ent, Point3d sourcePoint, Point3d targetPoint)
        {
            //声明一个图形对象
            Entity entR;
            // 判断图形对象的IsNewlyObject属性
            if (ent.IsNewObject)
            {
                // 计算变换矩阵
                Vector3d vector = sourcePoint.GetVectorTo(targetPoint);
                Matrix3d mt = Matrix3d.Displacement(vector);
                entR = ent.GetTransformedCopy(mt);
            }
            else
            {
                entR = ent.ObjectId.CopyEntity(sourcePoint, targetPoint);
            }
            return entR;
        }



        /// <summary>
        /// 旋转图形 图形在数据库中
        /// </summary>
        /// <param name="ent">图形对象</param>
        /// <param name="center">旋转中心</param>
        /// <param name="degree">旋转角度</param>
        public static void RotateEntity(this ObjectId entId, Point3d center, double degree)
        {
            // 当前图形数据库
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                // 打开块表
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                // 打开块表记录
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                //Entity ent = (Entity)trans.GetObject(entId, OpenMode.ForWrite);
                // 打开图形
                Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);
                // 计算变换矩阵
                Matrix3d mt = Matrix3d.Rotation(degree.DegreeToAngle(), Vector3d.ZAxis, center);
                ent.TransformBy(mt);
                // 提交事务处理
                trans.Commit();
            }
        }


        /// <summary>
        /// 旋转图形 图形不在数据库中
        /// </summary>
        /// <param name="ent">图形对象</param>
        /// <param name="center">旋转中心</param>
        /// <param name="degree">旋转角度</param>
        public static void RotateEntity(this Entity ent, Point3d center, double degree)
        {
            // 判断图形对象的IsNewlyObject属性
            if (ent.IsNewObject)
            {
                // 计算变换矩阵

                Matrix3d mt = Matrix3d.Rotation(degree.DegreeToAngle(), Vector3d.ZAxis, center);
                ent.TransformBy(mt);
            }
            else
            {
                ent.ObjectId.RotateEntity(center, degree);
            }
        }


        /// <summary>
        /// 镜像图形
        /// </summary>
        /// <param name="ent">图形对象的ObjectId</param>
        /// <param name="point1">第一个镜像点</param>
        /// <param name="point2">第二个镜像点</param>
        /// <param name="isEraseSource">是否删除原图形</param>
        /// <returns>返回新的图形对象  已加入图形数据库的情况</returns>
        public static Entity MirrorEntity(this ObjectId entId, Point3d point1, Point3d point2, bool isEraseSource)
        {
            // 声明一个图形对象用于返回
            Entity entR;
            // 计算镜像的变换矩阵
            Matrix3d mt = Matrix3d.Mirroring(new Line3d(point1, point2));
            Database db = entId.Database;
            // 打开事务处理
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                // 打开块表
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                // 打开块表记录
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                // 打开原图形
                Entity ent = (Entity)trans.GetObject(entId, OpenMode.ForWrite);
                // 这里得到的图形的IsNewLyObject = true
                entR = ent.GetTransformedCopy(mt);
                btr.AppendEntity(entR);
                trans.AddNewlyCreatedDBObject(entR, true);

                // 判断是否删除原对象
                if (isEraseSource)
                {
                    ent.Erase();
                }
                trans.Commit();
            }
            return entR;
        }


        /// <summary>
        /// 镜像图形
        /// </summary>
        /// <param name="ent">图形对象</param>
        /// <param name="point1">第一个镜像点</param>
        /// <param name="point2">第二个镜像点</param>
        /// <param name="isEraseSource">是否删除原图形</param>
        /// <returns>返回新的图形对象  已加入图形数据库的情况</returns>
        public static Entity MirrorEntity(this Entity ent, Point3d point1, Point3d point2, bool isEraseSource)
        {

            // 声明一个图形对象用于返回
            Entity entR;

            if (ent.IsNewObject == true)
            {
                Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    // 打开块表
                    BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                    // 打开块表记录
                    BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                    // 计算镜像的变换矩阵
                    Matrix3d mt = Matrix3d.Mirroring(new Line3d(point1, point2));
                    entR = ent.GetTransformedCopy(mt);
                    btr.AppendEntity(entR);
                    trans.AddNewlyCreatedDBObject(ent, true);
                    trans.Commit();
                }

            }
            else
            {
                entR = ent.ObjectId.MirrorEntity(point1, point2, isEraseSource);
            }
            return entR;
        }

        /// <summary>
        /// 缩放图形 图形已经加到图形数据库中
        /// </summary>
        /// <param name="entId">图形对象的ObjectId</param>
        /// <param name="basePoint">缩放的基点</param>
        /// <param name="facter">缩放比例</param>
        public static void ScaleEntity(this ObjectId entId, Point3d basePoint, double facter)
        {
            // 计算缩放矩阵
            Matrix3d mt = Matrix3d.Scaling(facter, basePoint);
            // 启动事务处理
            using (Transaction trans = entId.Database.TransactionManager.StartTransaction())
            {
                // 打开要缩放的图形对象
                Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);
                ent.TransformBy(mt);
                trans.Commit();
            }
        }


        /// <summary>
        /// 缩放图形 图形没有加到数据库中情况
        /// </summary>
        /// <param name="ent">图形对象</param>
        /// <param name="basePoint">缩放基点</param>
        /// <param name="facter">缩放比例</param>
        public static void ScaleEntity(this Entity ent, Point3d basePoint, double facter)
        {
            // 判断图形对象的IsNewlyObject属性
            if (ent.IsNewObject == true)
            {
                // 计算缩放矩阵
                Matrix3d mt = Matrix3d.Scaling(facter, basePoint);
                ent.TransformBy(mt);
            }
            else
            {
                ent.ObjectId.ScaleEntity(basePoint, facter);
            }
        }


        /// <summary>
        /// 删除图形对象
        /// </summary>
        /// <param name="entId">图形对象的ObjectId</param>
        public static void EraseEntity(this ObjectId entId)
        {
            // 打开事务处理
            using (Transaction trans = entId.Database.TransactionManager.StartTransaction())
            {
                // 打开要删除的图形对象
                Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);
                ent.Erase();
                trans.Commit();
            }
        }

        //public static void EraseEntity(this Entity ent)
        //{
        //    // 打开事务处理
        //    using (Transaction trans = ent.ObjectId.Database.TransactionManager.StartTransaction())
        //    {
        //        // 打开要删除的图形对象
        //        ent.ObjectId.GetObject(OpenMode.ForWrite);
        //        ent.Erase();
        //        trans.Commit();
        //    }
        //}
        /// <summary>
        /// 矩形阵列
        /// </summary>
        /// <param name="entId">图形对象的ObjectId</param>
        /// <param name="rowNum">行数</param>
        /// <param name="columnNum">列数</param>
        /// <param name="disRow">行间距</param>
        /// <param name="disColumn">列间距</param>
        /// <returns>List</returns>  已加入图形数据库
        public static List<Entity> ArrayRectEntity(this ObjectId entId, int rowNum, int columnNum, double disRow, double disColumn)
        {
            // 声明一个Entity类型集合 用于返回
            List<Entity> entList = new List<Entity>();

            // 当前图形数据库
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                // 打开块表
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                // 打开块表记录
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                //Entity ent = (Entity)trans.GetObject(entId, OpenMode.ForWrite);
                // 打开图形
                Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);

                // 计算变换矩阵
                for (int i = 0; i < rowNum; i++)
                {
                    for (int j = 0; j < columnNum; j++)
                    {
                        Matrix3d mt = Matrix3d.Displacement(new Vector3d(j * disColumn, i * disRow, 0));
                        Entity entA = ent.GetTransformedCopy(mt);
                        btr.AppendEntity(entA);
                        trans.AddNewlyCreatedDBObject(entA, true);
                        entList.Add(entA);
                    }
                }
                ent.Erase(); // 删除多余的图形
                // 提交事务处理
                trans.Commit();
            }
            return entList;
        }


        /// <summary>
        /// 矩形阵列
        /// </summary>
        /// <param name="entS">图形对象</param>
        /// <param name="rowNum">行数</param>
        /// <param name="columnNum">列数</param>
        /// <param name="disRow">行间距</param>
        /// <param name="disColumn">列间距</param>
        /// <returns>List</returns>  没有加入图形数据库
        public static List<Entity> ArrayRectEntity(this Entity entS, int rowNum, int columnNum, double disRow, double disColumn)
        {
            if (entS.IsNewObject == true)
            {
                // 声明一个Entity类型集合 用于返回
                List<Entity> entList = new List<Entity>();
                Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    // 打开块表
                    BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                    // 打开块表记录
                    BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                    for (int i = 0; i < rowNum; i++)
                    {
                        for (int j = 0; j < columnNum; j++)
                        {
                            Matrix3d mt = Matrix3d.Displacement(new Vector3d(j * disColumn, i * disRow, 0));
                            Entity entA = entS.GetTransformedCopy(mt);
                            btr.AppendEntity(entA);
                            trans.AddNewlyCreatedDBObject(entA, true);
                            entList.Add(entA);
                        }
                    }
                    trans.Commit();
                }
                return entList;
            }
            else
            {
                return entS.ArrayRectEntity(rowNum, columnNum, disRow, disColumn);
            }

        }


        /// <summary>
        /// 环形阵列
        /// </summary>
        /// <param name="entId">图形对象的ObjectId</param>
        /// <param name="num">图形数量</param>
        /// <param name="degree">中心点到各个图形的夹角</param>
        /// <param name="center">中心点</param>
        /// <returns>List</returns>  已经加入图形数据库
        public static List<Entity> ArrayPolarEntity(this ObjectId entId, int num, double degree, Point3d center)
        {
            // 声明一个List集合 用于返回
            List<Entity> entList = new List<Entity>();
            // 打开事务处理
            using (Transaction trans = entId.Database.TransactionManager.StartTransaction())
            {
                // 打开块表
                BlockTable bt = (BlockTable)trans.GetObject(entId.Database.BlockTableId, OpenMode.ForRead);
                // 打开块表记录
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                Entity ent = (Entity)entId.GetObject(OpenMode.ForWrite);
                // 限定阵列角度大小
                degree = degree > 360 ? 360 : degree;
                degree = degree < -360 ? 360 : degree;
                int divAngnum = num - 1;
                if (degree == 360 || degree == -360)
                {
                    divAngnum = num;
                }
                // 计算变换矩阵
                for (int i = 0; i < num; i++)
                {
                    Matrix3d mt = Matrix3d.Rotation((i * degree / divAngnum).DegreeToAngle(), Vector3d.ZAxis, center);
                    Entity entA = ent.GetTransformedCopy(mt);
                    btr.AppendEntity(entA);
                    trans.AddNewlyCreatedDBObject(entA, true);
                    entList.Add(entA);
                }
                ent.Erase();
                trans.Commit();
            }
            return entList;
        }



        /// <summary>
        /// 环形阵列
        /// </summary>
        /// <param name="ent">图形对象</param>
        /// <param name="num">图形数量</param>
        /// <param name="degree">中心点到各个图形的夹角</param>
        /// <param name="center">中心点</param>
        /// <returns>List</returns>
        public static List<Entity> ArrayPolarEntity(this Entity ent, int num, double degree, Point3d center)
        {
            if (ent.IsNewObject == true)
            {
                // 声明一个List集合 用于返回
                List<Entity> entList = new List<Entity>();
                Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    // 打开块表
                    BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                    // 打开块表记录
                    BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

                    degree = degree > 360 ? 360 : degree;
                    degree = degree < -360 ? -360 : degree;
                    int divAngnum = num - 1;
                    if (degree == 360 || degree == -360)
                    {
                        divAngnum = num;
                    }
                    for (int i = 0; i < num; i++)
                    {
                        Matrix3d mt = Matrix3d.Rotation((i * degree / divAngnum).DegreeToAngle(), Vector3d.ZAxis, center);
                        Entity entA = ent.GetTransformedCopy(mt);
                        btr.AppendEntity(entA);
                        trans.AddNewlyCreatedDBObject(entA, true);
                        entList.Add(entA);
                    }
                    trans.Commit();
                }
                return entList;
            }
            else
            {
                return ent.ObjectId.ArrayPolarEntity(num, degree, center);
            }

        }

    }
}

 

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AcTools
{
    public static partial  class GetTools
    {
        public static string GetString(string message)
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptResult pr;
            PromptStringOptions ps = new PromptStringOptions(message);
            pr = ed.GetString(ps);
            return pr.StringResult;
        }
        public static bool GetPoint(out Point3d point, string message)
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptPointResult pr;
            PromptPointOptions ps = new PromptPointOptions(message);
            pr = ed.GetPoint(ps);
            if (pr.Status == PromptStatus.OK)
            {
                point = pr.Value;
                return true;
            }
            else
            {
                ed.WriteMessage("\n用户取消了点的选择.");
                point = pr.Value;
                return false;
            }
        }

        public static void Erase(this Database db, Entity entity)
        {
           // Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                entity.ObjectId.GetObject(OpenMode.ForWrite);
                entity.Erase();
                trans.Commit();
            }
        }
        public static Entity  GetEntity(this Database db, string message)
        {
            Entity entity = null;
            Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptEntityResult per = editor.GetEntity("");
            if (per.Status == PromptStatus.OK)
            {
                //Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    entity = (Entity)per.ObjectId.GetObject(OpenMode.ForRead);
                }
            }
            return entity;
        }
        public static bool GetEntities(this Database db, out List<Curve> curves, string message)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            //List<ObjectId> objectIds = new List<ObjectId>();
            List<Curve> tempCurves = new List<Curve>();
            PromptSelectionOptions pso = new PromptSelectionOptions();
            pso.AllowDuplicates = true;
            PromptSelectionResult psr = ed.GetSelection(pso);
            if (psr.Status == PromptStatus.OK)
            {
                List<Curve> result = new List<Curve>();
                //SelectionSet selectionSet = psr.Value;
                ObjectId[] objs = psr.Value.GetObjectIds();
                using (Transaction tran = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
                {
                    foreach (var item in objs)
                    {
                        Entity entity = item.GetObject(OpenMode.ForRead) as Entity;
                        if (entity is Curve)
                        {
                            result.Add(entity as Curve);
                        }
                    }
                }
                curves = result;
                int a = result.Count;
                return a > 0;
            }
            else
            {
                curves = null;
                return false;
            }

        }
        public static List<T> SelectEntities<T>(this Database db) where T : Entity
        {
            List<T> result = new List<T>();
            Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptSelectionResult psr = editor.GetSelection();
            if (psr.Status == PromptStatus.OK)
            {
                ObjectId[] objectids = psr.Value.GetObjectIds();
                Database database = HostApplicationServices.WorkingDatabase;
                using (Transaction tran = database.TransactionManager.StartTransaction())
                {
                    foreach (var item in objectids)
                    {
                        Entity entity = item.GetObject(OpenMode.ForRead) as Entity;
                        if (entity is T)
                        {
                            result.Add(entity as T);
                        }

                    }
                }
            }
            return result;
        }
        public static ObjectId GetEntityInBlock(string message)
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptNestedEntityResult pr;
            PromptNestedEntityOptions pneo = new PromptNestedEntityOptions(message);
            pr = ed.GetNestedEntity(pneo);
            return pr.ObjectId;
        }
        public static List<Curve> ExplodePolyLine(Polyline polyline)
        {
            DBObjectCollection dbs = new DBObjectCollection();
            polyline.Explode(dbs);
            List<Curve> curves = new List<Curve>();
            foreach (Curve item in dbs)
            {
                curves.Add(item);
            }
            return curves;
        }
        public static Polyline CreatePolyLineByExtents3d(Extents3d extents)
        {
            Polyline pl = new Polyline();
            pl.AddVertexAt(0, new Point2d(extents.MinPoint.X, extents.MinPoint.Y), 0, 0, 0);
            pl.AddVertexAt(1, new Point2d(extents.MaxPoint.X, extents.MinPoint.Y), 0, 0, 0);
            pl.AddVertexAt(1, new Point2d(extents.MaxPoint.X, extents.MaxPoint.Y), 0, 0, 0);
            pl.AddVertexAt(2, new Point2d(extents.MinPoint.X, extents.MaxPoint.Y), 0, 0, 0);
            pl.Closed = true;
            return pl;
        }
        public  static Curve GetClosestCurve(this Database db, Point3d point, Vector3d vector, List<Curve> curves)
        {
            Ray ray = new Ray();
            ray.BasePoint = point;
            ray.UnitDir = vector;
            Point3d closestPoint = new Point3d();
            double closestDis = double.MaxValue;
            Curve closestCur = null;
            foreach (var curve in curves)
            {
                Point3dCollection pos = new Point3dCollection();
                ray.IntersectWith(curve, Intersect.OnBothOperands, pos, IntPtr.Zero, IntPtr.Zero);
                foreach (Point3d po in pos)
                {
                    double dis = po.DistanceTo(point);
                    if (dis < closestDis)
                    {
                        closestPoint = po;
                        closestDis = dis;
                        closestCur = curve;
                    }
                }
            }
            return closestCur;

        }

        public static void GetCurve(this Database db)
        {
            LineSegment3d line3d = new LineSegment3d(new Point3d(), new Point3d(10, 10, 0));
            Line line = new Line(new Point3d(5, 5, 0), new Point3d(20, 0, 0));
            line.SetFromGeCurve(line3d);
        }
        public static ObjectId GetCenterPl(this Database db)
        {
            List<Polyline> list = db.SelectEntities<Polyline>();
            if (list.Count < 2)
            {
                return ObjectId.Null;
            }

            Polyline polyline = list[0];
            Polyline polyline2 = list[1];
            int num = 1000;
            double num2 = polyline.Length / (double)num;
            double num3 = polyline2.Length / (double)num;
            Polyline polyline3 = new Polyline();
            for (int i = 0; i < num; i++)
            {
                Point3d pointAtDist = polyline.GetPointAtDist(num2 * (double)i);
                Point3d pointAtDist2 = polyline2.GetPointAtDist(num3 * (double)i);
                LineSegment3d lineSegment3d = new LineSegment3d(pointAtDist, pointAtDist2);
                Point3d midPoint = lineSegment3d.MidPoint;
                if (polyline3.NumberOfVertices >= 2)
                {
                    Vector3d vector3d = polyline3.GetPoint3dAt(polyline3.NumberOfVertices - 1) - polyline3.GetPoint3dAt(polyline3.NumberOfVertices - 2);
                    Vector3d vector3d2 = midPoint - polyline3.GetPoint3dAt(polyline3.NumberOfVertices - 1);
                    double angleTo = vector3d.GetAngleTo(Vector3d.XAxis, Vector3d.ZAxis);
                    double angleTo2 = vector3d2.GetAngleTo(Vector3d.XAxis, Vector3d.ZAxis);
                    if (Math.Abs(angleTo - angleTo2) < 0.001)
                    {
                        polyline3.SetPointAt(polyline3.NumberOfVertices - 1, midPoint.Convert2d(new Plane()));
                        continue;
                    }
                }

                polyline3.AddVertexAt(polyline3.NumberOfVertices, midPoint.Convert2d(new Plane()), 0.0, 0.0, 0.0);
                //polyline3.Closed = true;
            }

            return db.AddEntityToModeSpace(polyline3);
        }

        public static void Fangxiang()
        {  Database db = HostApplicationServices .WorkingDatabase; //if (db == null) return;
            Entity ent =db.GetEntity("");
            if (ent is Curve curve)
            {//Database db = HostApplicationServices .WorkingDatabase;
                int n = 2;
                double dis = curve.GetDistanceAtParameter(curve.EndParam) / n;
                for (int i = 0; i < n; i++)
                {
                    Point3d point = curve.GetPointAtDist(dis * i);
                    Vector3d vector = curve.GetFirstDerivative(point);
                    vector = vector.GetNormal() * dis;
                    Vector3d v1 = vector.RotateBy(Math.PI / 6 * 5, Vector3d.ZAxis);
                    Vector3d v2 = vector.RotateBy(Math.PI / 6 * 5, -Vector3d.ZAxis);
                    Polyline pl = new Polyline();
                    pl.ColorIndex = 2;
                    pl.AddVertexAt(0, (point + v1).Convert2d(new Plane()), 0, 0, 0);
                    pl.AddVertexAt(1, point.Convert2d(new Plane()), 0, 0, 0);
                    pl.AddVertexAt(2, (point + v2).Convert2d(new Plane()), 0, 0, 0);
                    db.AddEntityToModeSpace(pl);
                    Application.DocumentManager.MdiActiveDocument.Editor.GetInteger("");
                   db.Erase(pl) ;
                }
            }
        }
    }
}

 ↓↓↓

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山水CAD插件定制

你的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值