040集——CAD中放烟花(CAD—C#二次开发入门)

 效果如下:

单一颜色的烟花:

渐变色的火花:

 

namespace AcTools
{
    public class HH
    {
        public static TransientManager tm = TransientManager.CurrentTransientManager;
        public static Random rand = new Random();
        public static Vector3D G = new Vector3D(0,-10,0);// - Vector3D. * 10;
        public static Document doc => Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
        public static Editor ed => doc.Editor;
        [CommandMethod ("show")]
        public static void demo()
        {
            火花 h = new 火花();
            var psr = Z.ed.GetPoint("");
            while (psr.Status == PromptStatus.OK)
            {
                
                //h.添加火点(new Point3d(500, -100, 0));
                h.添加火点(new Point3d( psr.Value.X +200,psr.Value .Y-20 ,0));
                h.添加火点(new Point3d(psr.Value.X - 200, psr.Value.Y+20, 0));
                h.添加火点(new Point3d(psr.Value.X + 400, psr.Value.Y - 20, 0));
                h.添加火点(new Point3d(psr.Value.X - 400, psr.Value.Y + 20, 0));
                psr = Z.ed.GetPoint("");
            }
        }
    }

   public class 火花:IDisposable
    { 
        Timer timer;
        List<火星>火星s=new List<火星> ();
        //int k = 0;
    public 火花()
    {
        timer = new Timer();
        火星s = new List<火星>();
        timer.Interval = 30;//相当于多久更新一下屏幕
        timer.Tick += 火星飞行事件;
        timer.Start();
    }
    public void 添加火点(Point3d pt)
    {
        int n = new Random().Next(26, 40);//烟花的个数
        var sudu = new Random().Next(16,27);//速度
        int ys = HH.rand.Next(0, 5);//颜色
        Color cc;
        switch (ys)
        {
            case 0:cc  =Color.FromRgb(255,0,0); break;//红
            case 1: cc = Rainbow(0, 255, 0); break;//绿
            case 2: cc = Rainbow(255, 255, 0); break;//黄
            case 3: cc = Rainbow(255, 0, 255); break;//紫
            case 4: cc = Rainbow(255, 186, 0); break;//橙
            default: cc = Rainbow(0, 0, 255); break;//蓝
            }
        double 衰减速度 = HH.rand.NextDouble() * 0.05 + 0.95;
        for (int i = 0; i < n; i++)
        {
                var vt = new Vector3d(HH.rand.NextDouble() - 0.5, HH.rand.NextDouble() - 0.5, HH.rand.NextDouble() - 0.5);
            火星s.Add(new 火星(pt, vt, sudu, cc, 衰减速度));
        }
        
    }
    public static Color  Rainbow(int k, int min, int max)
    {
    

            return Color.FromRgb((byte)k, (byte)min, (byte)max);
        }
    private  void 火星飞行事件(object sender, EventArgs e)
    {
        foreach (var h in 火星s)
        {
            h.运动();
        }
        List<Circle> cirs = new List<Circle>();
            for (int i = 火星s.Count-1; i  >=0   ; i--)
            {
                if (火星s[i].灭)
                {
                    cirs.AddRange(火星s[i].cirs);
                    火星s.RemoveAt(i);
                }
            }
            foreach (var c  in cirs)
            {
                HH.tm.EraseTransient(c, new IntegerCollection());
                HH.ed.UpdateScreen();
                System.Windows .Forms.Application.DoEvents();
            }
        }
    public void Dispose()
    {
        timer.Tick -= 火星飞行事件;
        火星s.Clear();
        timer.Stop(); 
        timer.Dispose();
    }
   }
    public delegate Autodesk.AutoCAD.Colors.Color 颜色(double k,double min,double max);
    class 火星
    {
        Random rand = new Random();
        public bool 灭;
        public List<Circle>cirs = new List<Circle>();
        Circle cir;
        int k;
        double 速度;
        int 总数量;
        Color 颜色渐变;
        double 衰减速度;
        Vector3D 方向;
 ////////////*
///
///
///

        public void 运动()
        {
            k++;
            var vt = 方向.GetNormal() * 速度;
            var g = new Vector3D(0, -10, 0);
            vt = vt + HH.G;
            cir.Center = cir.Center + vt;
            Circle cir1 = cir.Clone() as Circle;
            cir1.Color = 颜色渐变;
            cirs.Add(cir1);
            HH.tm.AddTransient(cir1, TransientDrawingMode.Main, 0, new IntegerCollection());
            速度 *= 衰减速度;
            if (k > 总数量 || 速度 < 0.1)
            {
                灭 = true;
            }
        }

    }
}

finally

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

山水CAD插件定制

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

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

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

打赏作者

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

抵扣说明:

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

余额充值