039集——渐变色之:CAD中画彩虹()(CAD—C#二次开发入门)
(来左边儿 跟我一起画个龙,在你右边儿 画一道彩虹 ~~~~~~~~~~~ )
效果如下:
以下展示部分颜色源码:
namespace AcTools
{
public class Class1
{
public Wform.Timer timer;//定时器需建在类下面
public static DateTime startTime;
[CommandMethod("xx")]
public void Demo()
{
红到黄(255, 1000.0);
黄到绿(255, 1000.0 + 255 * 1);
绿到蓝(255, 1000.0 + 255 * 2);
蓝到紫(255, 1000.0 + 255 * 3);
timer = new Wform.Timer();
timer.Interval = 2500;//相当于多久更新一下屏幕
timer.Tick += Timer_Tick;//定时器关联事件;
startTime = DateTime.Now;
timer.Start();
//红到紫(255, 1000.0 + 255 * 4);
}
public void Timer_Tick(object sender, EventArgs e)//一定时间间隔触发的事件
{
TimeSpan elapsed = DateTime.Now - startTime;
红到紫(255, 1000.0 + 255 * 4);
if (elapsed.TotalSeconds > 10)
{
timer.Stop();
timer.Dispose();
Z.ed.WriteMessage("over\n");
}
Z.ed.UpdateScreen();
}
public void 红到紫(double num, double radius)
{
//红到紫 255, 0 ,0 ---- 255 ,0, 255
List<Circle> circles = new List<Circle>();//画圆
List<Arc> arcs = new List<Arc>();//画弧
for (int i = 1; i <= num; i++)
{
Circle circle = new Circle()
{
Center = new Point3d(500, 1000, 0),
Radius = radius + i,
Color = (i == 0) ? Color.FromRgb(255, 0, 0) : Color.FromRgb(255, 0,(byte)(0 + i / num * 255) ),
};
circles.Add(circle);
}
arcs = Halfcircle(circles);
for (int i = 0; i < 5; i++)
{
List<Entity> ents = arcs.Cast<Entity>().ToList();//list类型强转
Flash flash = new Flash(ents );//创建瞬态
flash.FlashUpdate(ents);//更新瞬态
//Z.ed.GetPoint("");//
foreach (Entity item in ents)
{
item.ChangeEntityColor(i);
}
flash.FlashUpdate(ents);//更新瞬态
Thread.Sleep(500);
Z.ed.UpdateScreen();
flash.FlashlistErase();//删除瞬态
}
//Z.db.AddEntityToModeSpace(arcs.ToArray());
}
public void 红到黄(double num,double radius)
{//红到黄 255 0 0 - 255 255 0
List<Circle> circles = new List<Circle>();//画圆
List<Arc> arcs = new List<Arc>();//画弧
for (int i = 1; i <= num; i++)
{
Circle circle = new Circle()
{
Center = new Point3d(500, 1000, 0),
Radius = radius + i ,
Color = (i ==0)? Color.FromRgb(255, 0, 0) : Color.FromRgb(255, (byte)(0 + i/ num* 255), 0),
};
circles.Add(circle);
}
arcs = Halfcircle(circles);
Z.db.AddEntityToModeSpace(arcs.ToArray());
}
public void 黄到绿(double num, double radius)
{//黄到绿255 ,255 ,0 - 0, 255,0
List<Circle> circles = new List<Circle>();//画圆
List<Arc> arcs = new List<Arc>();//画弧
for (int i = 1; i <= num; i++)
{
Circle circle = new Circle()
{
Center = new Point3d(500, 1000, 0),
Radius = radius + i,
Color = (i == 1) ? Color.FromRgb(255, 255, 0) : Color.FromRgb((byte)(255 - i / num * 255), 255,0),
};
circles.Add(circle);
}
arcs = Halfcircle(circles);
Z.db.AddEntityToModeSpace(arcs.ToArray());
}
public void 绿到蓝(double num, double radius)
{//绿到蓝0, 255,0 ---- 0 ,0 ,255
List<Circle> circles = new List<Circle>();//画圆
List<Arc> arcs = new List<Arc>();//画弧
for (int i = 1; i <= num; i++)
{
Circle circle = new Circle()
{
Center = new Point3d(500, 1000, 0),
Radius = radius + i,
Color = (i == 1) ? Color.FromRgb(0,255, 255 ) : Color.FromRgb(0,(byte)(255 - i / num * 255), (byte)(0 + i / num * 255)),
};
circles.Add(circle);
}
arcs = Halfcircle(circles);
Z.db.AddEntityToModeSpace(arcs.ToArray());
}
public void 蓝到紫(double num, double radius)
{//蓝到紫 0 ,0 ,255 - 255, 0, 255
List<Circle> circles = new List<Circle>();//画圆
List<Arc> arcs = new List<Arc>();//画弧
for (int i = 1; i <= num; i++)
{
Circle circle = new Circle()
{
Center = new Point3d(500, 1000, 0),
Radius = radius + i,
Color = (i == 1) ? Color.FromRgb(0, 0, 255) : Color.FromRgb((byte)(0 + i / num * 255), 0, 255),
};
circles.Add(circle);
}
arcs = Halfcircle(circles);
Z.db.AddEntityToModeSpace(arcs.ToArray());
}
public static List<Arc> Halfcircle(List<Circle> circles)
{
List<Arc> arcs = new List<Arc>();//画弧
if (!( circles is null )&& circles.Count >0)
{
foreach (Circle circle in circles)
{
arcs.Add(new Arc() { Center = circle.Center,Radius = circle .Radius,
StartAngle = 0,Color = circle .Color, EndAngle = Math.PI });
}
}
return arcs;
}
}
}
namespace AcTools
{
public class Class1
{
[CommandMethod("xx")]
public void Demo()
{
红到黄(255, 1000.0);
黄到绿(255, 1000.0 + 255 * 1);
绿到蓝(255, 1000.0 + 255 * 2);
蓝到紫(255, 1000.0 + 255 * 3);
}
public void 红到紫(double num, double radius)
{//红到紫 255, 0 ,0 ---- 255 ,0, 255
List<Circle> circles = new List<Circle>();//画圆
List<Arc> arcs = new List<Arc>();//画弧
for (int i = 1; i <= num; i++)
{
Circle circle = new Circle()
{
Center = new Point3d(500, 1000, 0),
Radius = radius + i,
Color = (i == 0) ? Color.FromRgb(255, 0, 0) : Color.FromRgb(255, 0,(byte)(0 + i / num * 255) ),
};
circles.Add(circle);
}
arcs = Halfcircle(circles);
Z.db.AddEntityToModeSpace(arcs.ToArray());
}
public void 红到黄(double num,double radius)
{//红到黄 255 0 0 - 255 255 0
List<Circle> circles = new List<Circle>();//画圆
List<Arc> arcs = new List<Arc>();//画弧
for (int i = 1; i <= num; i++)
{
Circle circle = new Circle()
{
Center = new Point3d(500, 1000, 0),
Radius = radius + i ,
Color = (i ==0)? Color.FromRgb(255, 0, 0) : Color.FromRgb(255, (byte)(0 + i/ num* 255), 0),
};
circles.Add(circle);
}
arcs = Halfcircle(circles);
Z.db.AddEntityToModeSpace(arcs.ToArray());
}
public void 黄到绿(double num, double radius)
{//黄到绿255 ,255 ,0 - 0, 255,0
List<Circle> circles = new List<Circle>();//画圆
List<Arc> arcs = new List<Arc>();//画弧
for (int i = 1; i <= num; i++)
{
Circle circle = new Circle()
{
Center = new Point3d(500, 1000, 0),
Radius = radius + i,
Color = (i == 1) ? Color.FromRgb(255, 255, 0) : Color.FromRgb((byte)(255 - i / num * 255), 255,0),
};
circles.Add(circle);
}
arcs = Halfcircle(circles);
Z.db.AddEntityToModeSpace(arcs.ToArray());
}
public void 绿到蓝(double num, double radius)
{//绿到蓝0, 255,0 ---- 0 ,0 ,255
List<Circle> circles = new List<Circle>();//画圆
List<Arc> arcs = new List<Arc>();//画弧
for (int i = 1; i <= num; i++)
{
Circle circle = new Circle()
{
Center = new Point3d(500, 1000, 0),
Radius = radius + i,
Color = (i == 1) ? Color.FromRgb(0,255, 255 ) : Color.FromRgb(0,(byte)(255 - i / num * 255), (byte)(0 + i / num * 255)),
};
circles.Add(circle);
}
arcs = Halfcircle(circles);
Z.db.AddEntityToModeSpace(arcs.ToArray());
}
public void 蓝到紫(double num, double radius)
{//蓝到紫 0 ,0 ,255 - 255, 0, 255
List<Circle> circles = new List<Circle>();//画圆
List<Arc> arcs = new List<Arc>();//画弧
for (int i = 1; i <= num; i++)
{
Circle circle = new Circle()
{
Center = new Point3d(500, 1000, 0),
Radius = radius + i,
Color = (i == 1) ? Color.FromRgb(0, 0, 255) : Color.FromRgb((byte)(0 + i / num * 255), 0, 255),
};
circles.Add(circle);
}
arcs = Halfcircle(circles);
Z.db.AddEntityToModeSpace(arcs.ToArray());
}
public static List<Arc> Halfcircle(List<Circle> circles)
{
List<Arc> arcs = new List<Arc>();//画弧
if (!( circles is null )&& circles.Count >0)
{
foreach (Circle circle in circles)
{
arcs.Add(new Arc() { Center = circle.Center,Radius = circle .Radius,
StartAngle = 0,Color = circle .Color, EndAngle = Math.PI });
}
}
return arcs;
}
}
}
public static ObjectId[] AddEntityToModeSpace(this Database db, params Entity[] ent)
{
// 声明ObjectId 用于返回
ObjectId[] entId = new ObjectId[ent.Length];
// 开启事务处理
using (DocumentLock acLckDoc = Z.doc.LockDocument())
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 打开块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
// 打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
for (int i = 0; i < ent.Length; i++)
{
// 将图形添加到块表记录
if (ent[i].IsNewObject)
{
entId[i] = btr.AppendEntity(ent[i]);
// 更新数据信息
trans.AddNewlyCreatedDBObject(ent[i], true);
}
// Z.ed.Redraw(ent[i]);//动态显示
}
// 提交事务
trans.Commit();
}
}
return entId;
}
public static Database db
{
get { return HostApplicationServices.WorkingDatabase; }
}
Arc arc = new Arc()
{
Center = new Point3d(500,500+i,0),
Radius = 500,StartAngle = Math.PI*0.1+i*0.01,
EndAngle=Math.PI*0.9+i * 0.01,
//StartPoint = new Point3d(0, i, 0),
//EndPoint = new Point3d(1000, i, 0),
Color = Color.FromRgb(255, (byte)i, 0),
};
public void xx()
{//红到黄 255 0 0 - 255 255 0
List<Line> lines = new List<Line>();//画线
for (int i = 0; i < 255; i++)
{
Line line = new Line() { StartPoint = new Point3d(0,i,0),
EndPoint = new Point3d(1000,i, 0),
Color = Color .FromRgb(255, (byte)i, 0),
};
lines.Add(line);
}
Z.db.AddEntityToModeSpace(lines.ToArray());
List<Arc> arcs = new List<Arc>();//画圆弧
for (int i = 0; i < 255; i++)
{
Arc arc = new Arc()
{
Center = new Point3d(500,1000+i,0),
Radius = 500,StartAngle = Math.PI*0.1-i*0.01,
EndAngle=Math.PI*0.9+i * 0.01,
//StartPoint = new Point3d(0, i, 0),
//EndPoint = new Point3d(1000, i, 0),
Color = Color.FromRgb(255, (byte)i, 0),
};
arcs.Add(arc);
}
Z.db.AddEntityToModeSpace(arcs.ToArray());
}
finally: