026集——CAD动态效果—瞬态实现——vs CAD二次开发
瞬态类特点是不写入dwg图形的数据库中,只是临时显示的效果。
通过按ESC键更新显示,效果如下:
首先创建瞬态类,代码如下:
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace AcTools
{public class Flash {public FlashEntity flash = null;public Entity ent = null;public Flash(Circle ent){this.ent = ent;flash = new FlashEntity(ent);Transient.CapturedDrawable = flash;TransientManager.CurrentTransientManager.AddTransient(flash,TransientDrawingMode.Main, 128, new IntegerCollection());}public void FlashUpdate(Circle ent){if (ent == null){this.ent = ent;flash = new FlashEntity(ent);Transient.CapturedDrawable = flash;TransientManager.CurrentTransientManager.AddTransient(flash,TransientDrawingMode.Main, 128, new IntegerCollection());}else{this.ent = ent;flash._ent = ent;TransientManager.CurrentTransientManager.UpdateTransient(flash, new IntegerCollection());}}public void FlashErase(){if (flash == null) return;flash._ent = null;TransientManager.CurrentTransientManager.EraseTransient(flash, new IntegerCollection());flash = null;}}public class FlashEntity : Transient{public Entity _ent;public FlashEntity(Entity ent){_ent = ent;}protected override int SubSetAttributes(DrawableTraits traits){traits.FillType = FillType.FillAlways;return (int)DrawableAttributes.IsAnEntity;}protected override void SubViewportDraw(ViewportDraw vd){vd.Geometry.Draw(_ent);}protected override bool SubWorldDraw(WorldDraw wd){return false;}}
}
然后开始用瞬态实现动画:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AcTools;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Colors;
using System.Runtime.CompilerServices;
using Wform = System.Windows.Forms;
using System.IO;
using System.Windows.Forms;
using Excel= Microsoft.Office.Interop.Excel.Application;
using Autodesk.AutoCAD.Windows;
//using UserControl = System.Windows.Controls.UserControl;
//PaletteSet integration
using System.Windows.Forms.Integration;
using Autodesk.AutoCAD.Internal;namespace AcTools
{public class Class1{Flash _flash;//定义一个瞬态flash变量[CommandMethod("xx")]public void XX(){List<Flash> listflash = new List<Flash>();//实例化一个瞬态flashList<Circle> listcircle = new List<Circle>();for (int i = 0; i < 30; i++)//创建50个圆并加入flash列表中{Circle circle = new Circle() { Radius = i+1,ColorIndex = i+1};listcircle.Add(circle);_flash = new Flash(listcircle[i]);listflash.Add(_flash); }//Z.Zoom();Z.ed.GetPoint("");//20次esc键,观看动态效果for (int j = 0; j < 20; j++){ //瞬态中50个圆更新属性for (int i = 0; i < 30; i++){listcircle[i].Radius = listcircle[i].Radius+1;listcircle[i].ColorIndex = 255 - listcircle[i].ColorIndex;listflash[i].FlashUpdate(listcircle[i]);}Z.ed.GetPoint("");}for (int i = 0; i < 30; i++){listflash[i].FlashErase();//删除瞬态}}}
}