零件分类——c#

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.GraphicsInterface;
using IFoxCAD.Cad;
using System.Globalization;
using System.Security.Cryptography;
using System.Windows.Forms;
using System.Windows.Markup;
using Path = System.IO.Path;
namespace IfoxDemo
{
    public class 零件()
    {
        [CommandMethod("xx")]
        public void 零件分类()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            var curs = Z.PickList(true);
            if (curs.Count ==0) return;
            List<小线线> 小线线集合 = new List<小线线>();
            curs.ForEach(x => 小线线集合.Add(new 小线线(x)));
            小线线集合.Sort((x1, x2) => { 
                double len1 = x1.GetLength();
                double len2 = x2.GetLength();
                return  (len2.CompareTo(len1)); //降序
            });
            //循环遍历线,找出上级和下级关系
            for (int i = 0; i < 小线线集合.Count; i++)
            {
                //第一层循环只遍历闭合的线,不闭合的线不需要遍历了,肯定不是最上级
                if (!小线线集合[i].closed) continue;
                //有上级了就不用比较了,已经找分组归属了,不需要知道下级是谁。
                if (小线线集合[i].上级.Count > 0) continue;
                for ( int j = i+1; j < 小线线集合.Count; j++)
                {
                    //如果包围盒没交集,就不需要比较了
                    if (!小线线集合[i].cur.GeometricExtents.Extents3dHit(小线线集合[j].cur.GeometricExtents)) continue;
                    //判断线完全在曲线内
                    var a = 小线线集合[j].cur.MiddlePoint().Point3dInCurveRayway(小线线集合[i].cur);
                    var b = 小线线集合[j].cur.StartPoint.Point3dInCurveRayway(小线线集合[i].cur);
                    if ( a || b  )//因为图有问题,有一个点在就算
                        //if (小线线集合[j].cur.线完全在曲线内射线法3个点(小线线集合[i].cur))
                    {
                        小线线集合[i].下级.Add(小线线集合[j]);
                        小线线集合[j].上级.Add(小线线集合[i]);
                    }
                }
            }
            //找到每组的老大(最上级),改颜色,或其他操作
            var 老大们 = 小线线集合.Where(x => x.上级.Count == 0).ToList();
            int count = 0;
             List 一组curve = new List();
            foreach (var 老大 in 老大们)
            {
                一组curve.Add(老大.cur);
                var colorindex = (count + 1) % 7;
                老大.cur.ColorIndex = colorindex;
                //老大.cur.Color = ColorName.RandColor;
                //老大.cur.LineWeight = LineWeight.LineWeight050;
                //老大.cur.Layer = "0";
                foreach (var item in 老大.下级)
                {
                    item.cur.ColorIndex = 老大.cur.ColorIndex;
                   // item.cur.ColorIndex = count;
                    一组curve.Add(item.cur);
                }
                count++;
                Z.eAddtoMS(一组curve);
               // db.AddEntityToModeSpace(一组curve.ToArray() );
                一组curve.Clear();
            }
        }
    }
    public class  小线线
    {
        public Curve cur;
        public bool closed;
        public double GetLength(){return cur.GetLength(); }
        public List<小线线> 上级 = new List<小线线>();
        public List<小线线> 下级 = new List<小线线>();
        public 小线线(Curve cur)
        {
           this.cur = cur;
           this.closed = cur.Closed||cur.StartPoint.isEqualspt(cur.EndPoint);
        }
    }
}

public static bool 包围盒包含或相交(this Extents3d ext1, Extents3d ext2)
{
    return
     ext1.MaxPoint.X >= ext2.MinPoint.X &&
     ext1.MinPoint.X <= ext2.MaxPoint.X &&
     ext1.MaxPoint.Y >= ext2.MinPoint.Y &&
     ext1.MinPoint.Y <= ext2.MaxPoint.Y ||
     ext2.MaxPoint.X >= ext1.MinPoint.X &&
     ext2.MinPoint.X <= ext1.MaxPoint.X &&
     ext2.MaxPoint.Y >= ext1.MinPoint.Y &&
     ext2.MinPoint.Y <= ext1.MaxPoint.Y;
}

你可能感兴趣的:(CAD,C#二次开发,分类,c#,数据挖掘)