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