C# LINQ数据检索角度大于PI的直线

using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
[assembly:CommandClass(typeof(LINQ数据检索角度大于PI的直线.Class1))]

namespace LINQ数据检索角度大于PI的直线{

    public class Class1
    {
        [CommandMethod("Linq")]
        public void Linq()
        {
            FilterType[] tps = new FilterType[] { FilterType.Line };
            DBObjectCollection dc = GetSelection(tps);
            List<Line> ls = new List<Line>();
            foreach (DBObject obj in dc)
                ls.Add((Line)obj);
            int n = (from g in ls where g.Angle > Math.PI select g).Count<Line>();
            Application.ShowAlertDialog("选中的直线中角度大于л的有" + n.ToString() + "条");
        }

        /// <summary>
        /// 类型过滤枚举类
        /// </summary>
        public enum FilterType
        {
            Curve, Dimension, PolyLine, BlockRef, Circle, 
Line, Arc, DBText, MText, Polyline3d, Surface, Region, Solid3d, 
Hatch, Helix, DBPoint
        }

        /// <summary>
        /// 过滤选择集
        /// </summary>
        /// <param name="tps">过滤类型</param>
        /// <returns>选中的对象集合</returns>
        public static DBObjectCollection GetSelection(FilterType[] tps)
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            Entity entity = null;
            DBObjectCollection entityCollection = new DBObjectCollection();
            PromptSelectionOptions selops = new PromptSelectionOptions();
            TypedValue[] filList = new TypedValue[tps.Length + 2];
            filList[0] = new TypedValue((int)DxfCode.Operator, "<or");
            filList[tps.Length + 1] = new TypedValue((int)DxfCode.Operator, "or>");
            for (int i = 0; i < tps.Length; i++)
            {
                filList[i + 1] = new TypedValue((int)DxfCode.Start, tps[i].ToString());
            }
            SelectionFilter filter = new SelectionFilter(filList);
            PromptSelectionResult optSel = ed.GetSelection(filter);

            if (optSel.Status == PromptStatus.OK)
            {
                using (Transaction transaction = db.TransactionManager.StartTransaction())
                {
                    SelectionSet SS = optSel.Value;
                    foreach (ObjectId id in SS.GetObjectIds())
                    {
                        entity = (Entity)transaction.GetObject(id, OpenMode.ForWrite, true);
                        if (entity != null)
                            entityCollection.Add(entity);
                    }
                    transaction.Commit();
                }
            }
            return entityCollection;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/laocooon/article/details/121463857