参考鸿视频实现,代码如下:
主类
using System.IO.Ports;
using System.Threading;
using System.Windows.Controls;
using static IFoxDemo.进度条class;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar;
//[assembly: CommandClass(typeof(IFoxDemo.进度条class))]//只允许此类快捷键命令
namespace IFoxDemo
{
public class 进度条class
{
public static bool isover;
[CommandMethod("xx")]
public static void tt()
{
isover = false;
Document doc = Env.Document;
using var tr = new DBTrans();
//瞬态进度条
int n = 10000;//总循环次数
int m = 100;//进度条更新次数
int k = 0;
屏幕进度条 jd = new 屏幕进度条(m, "正在运行中......");
for (int i = 0; i < n; i++)
{
if (i % (n / m) == 0)
{
jd.Run();
k++;
var mydouble = (double)k / m * 100;
var mystr = mydouble.ToString() + "%";
mystr.Print();//命令行输出进度
Thread.Sleep(20);//暂停50毫秒
//Env.Editor.Redraw();//ifox 写法 。
System.Windows.Forms.Application.DoEvents();//影响程序速度
//暂停调试.暂停();
}
}
isover = true;
jd.Stop();
"ok\n".Print();
}
class 屏幕进度条
{
double max;//总进度
double tick;//当前进度
string str;
TransientManager tm;
Polyline 进度多段线, 总进度线;
Extents3d 屏幕包围盒;
double 屏幕高度, 屏幕宽度, 进度条长度, 进度条高度, 百分比高度, 提示高度;
Point3d 起点, 终点, 当前点, 百分比位置点, 提示位置点;
MText 百分比;
MText 提示文字;
public 屏幕进度条(int max, string str)
{
this.max = max;
this.str = str;
tm = TransientManager.CurrentTransientManager;
Document doc = Env.Document;//
doc.BeginDocumentClose += Doc_BeginDocumentClose;
doc.CloseWillStart += Doc_CloseWillStart;
//Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
屏幕包围盒 = 视口包围盒.GetViewExtents3d();
屏幕宽度 = 屏幕包围盒.MaxPoint.X - 屏幕包围盒.MinPoint.X;
屏幕高度 = 屏幕包围盒.MaxPoint.Y - 屏幕包围盒.MinPoint.Y;
进度条高度 = 屏幕高度 * 0.02;
进度条长度 = 屏幕宽度 * (1 - 0.618) * 0.618;
起点 = new Point3d(屏幕包围盒.MinPoint.X + 屏幕宽度 * 0.618, 屏幕包围盒.MinPoint.Y + 屏幕高度 * 0.2, 0);
终点 = new Point3d(起点.X + 进度条长度, 起点.Y, 0);
当前点 = 起点;
进度多段线 = new Polyline();
进度多段线.AddVertexAt(0, 起点.Point2d(), 0, 0, 0);
进度多段线.AddVertexAt(1, 起点.Point2d(), 0, 0, 0);
进度多段线.Color = ColorName.huang;
进度多段线.ConstantWidth = 进度条高度;
总进度线 = new Autodesk.AutoCAD.DatabaseServices.Polyline();
总进度线.AddVertexAt(0, 起点.Point2d(), 0, 0, 0);
总进度线.AddVertexAt(1, 终点.Point2d(), 0, 0, 0);
总进度线.Color = ColorName.lv;
百分比高度 = 进度条高度 * 0.7;
百分比位置点 = 当前点 + new Vector3d(0, 进度条高度 * 1.6, 0);
//百分比 = new MText();
//百分比.SetDatabaseDefaults(Env.Database);
//百分比.Contents = "0.0%";
//百分比.Location = 百分比位置点;
//百分比.Height = 百分比高度;
百分比 = MTextEx.CreateMText(百分比位置点, "0.0%", 百分比高度, action: t => t.Color = ColorName.lv);//ifox写法
提示高度 = 进度条高度;
提示位置点 = 起点 - new Vector3d(0, 进度条高度, 0);
提示文字 = MTextEx.CreateMText(提示位置点, str, 提示高度, action: t => t.Color = ColorName.lv);//ifox写法
tm.AddTransient(总进度线, TransientDrawingMode.DirectTopmost, 0, new IntegerCollection());
tm.AddTransient(进度多段线, TransientDrawingMode.DirectTopmost, 0, new IntegerCollection());
tm.AddTransient(百分比, TransientDrawingMode.DirectTopmost, 0, new IntegerCollection());
tm.AddTransient(提示文字, TransientDrawingMode.DirectTopmost, 0, new IntegerCollection());
tick = 0;
doc.ViewChanged += Doc_ViewChanged;
}
private void Doc_CloseWillStart(object sender, EventArgs e)
{
Stop();
}
private void Doc_BeginDocumentClose(object sender, DocumentBeginCloseEventArgs e)
{
Stop();
}
public void Doc_ViewChanged(object sender, EventArgs e)
{
if (!isover)
{
//更新瞬态();
屏幕包围盒 = 视口包围盒.GetViewExtents3d();
屏幕宽度 = 屏幕包围盒.MaxPoint.X - 屏幕包围盒.MinPoint.X;
屏幕高度 = 屏幕包围盒.MaxPoint.Y - 屏幕包围盒.MinPoint.Y;
进度条高度 = 屏幕高度 * 0.02;
进度条长度 = 屏幕宽度 * (1 - 0.618) * 0.618;
起点 = new Point3d(屏幕包围盒.MinPoint.X + 屏幕宽度 * 0.618, 屏幕包围盒.MinPoint.Y + 屏幕高度 * 0.2, 0);
终点 = 起点 + new Vector3d(进度条长度, 0, 0); //new Point3d(起点.X + 进度条长度, 起点.Y, 0);
当前点 = 起点 + new Vector3d(tick / max * 进度条长度, 0, 0);
//进度多段线 = new Polyline();
进度多段线.SetPointAt(0, 起点.Point2d());
进度多段线.SetPointAt(1, 当前点.Point2d());
//进度多段线.Color = ColorName.huang;
进度多段线.ConstantWidth = 进度条高度 * 0.7;
//总进度线 = new Autodesk.AutoCAD.DatabaseServices.Polyline();
总进度线.SetPointAt(0, 起点.Point2d());
总进度线.SetPointAt(1, 终点.Point2d());
总进度线.Color = ColorName.lv;
百分比高度 = 进度条高度 * 0.7;
百分比位置点 = 当前点 + new Vector3d(0, 进度条高度 * 1.6, 0);
//百分比 = MTextEx.CreateMText(百分比位置点, "0.0%", 百分比高度, action: t => t.Color = ColorName.lv);//ifox写法
百分比.Location = 当前点 + new Vector3d(0, 进度条高度 * 1.6, 0);
提示高度 = 进度条高度;
提示位置点 = 起点 - new Vector3d(0, 进度条高度, 0);
//提示文字 = MTextEx.CreateMText(提示位置点, str, 提示高度, action: t => t.Color = ColorName.lv);//ifox写法
提示文字.Location = 提示位置点;
提示文字.Height = 提示高度;
更新瞬态();
//tm.UpdateTransient(总进度线, new IntegerCollection());
//tm.UpdateTransient(进度多段线, new IntegerCollection());
//tm.UpdateTransient(百分比, new IntegerCollection());
//tm.UpdateTransient(提示文字, new IntegerCollection());
//System.Windows.Forms.Application.DoEvents();//影响程序速度
Env.Editor.UpdateScreen();
}
}
private void 更新瞬态()
{
tm.UpdateTransient(总进度线, new IntegerCollection());
tm.UpdateTransient(进度多段线, new IntegerCollection());
tm.UpdateTransient(百分比, new IntegerCollection());
tm.UpdateTransient(提示文字, new IntegerCollection());
}
public void Run()
{
tick++;
if (tick >= max) Stop();
当前点 = 起点 + new Vector3d(tick / max * 进度条长度, 0, 0);
进度多段线.SetPointAt(1, 当前点.Point2d());
百分比.Location = 当前点 + new Vector3d(0, 进度条高度 * 1.6, 0);
百分比.Contents = (tick / max * 100).ToString("0") + "%";
tm.UpdateTransient(进度多段线, new IntegerCollection());
tm.UpdateTransient(百分比, new IntegerCollection());
//Env.Editor.Redraw();//ifox 写法 。
Document doc1 = Env.Document;
// doc1.ViewChanged += Doc1_ViewChanged;
// System.Windows.Forms.Application.DoEvents();//影响程序速度
Env.Editor.UpdateScreen();//比redraw更新瞬态效果好
}
public void Stop()
{
tm.EraseTransient(总进度线, new IntegerCollection());
tm.EraseTransient(进度多段线, new IntegerCollection());
tm.EraseTransient(百分比, new IntegerCollection());
tm.EraseTransient(提示文字, new IntegerCollection());
//System.Windows.Forms.Application.DoEvents();//影响程序速度
Env.Editor.UpdateScreen();
}
//MText MText(string textstring,Point3d location ,double height)
//{//代码省略
// return null;
//}
}
}
}
colorname
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IFoxDemo
{
public static class ColorName
{
public static Color 红 = Color.FromRgb(255, 0, 0);
public static Color 绿 = Color.FromRgb(0, 255, 0);
public static Color 蓝 = Color.FromRgb(0, 0, 255);
public static Color 黄 = Color.FromRgb(255, 255, 0);
public static Color 洋 = Color.FromRgb(255, 0, 255);
public static Color 青 = Color.FromRgb(0, 255, 255);
public static Color 白 = Color.FromRgb(255, 255, 255);
public static Color 黑 = Color.FromRgb(0, 0, 0);
public static Color 灰127 = Color.FromRgb(127, 127, 127);
public static Color 灰50 = Color.FromRgb(50, 50, 50);
public static Color 灰200 = Color.FromRgb(200, 200, 200);
public static Color 克莱因蓝 = Color.FromRgb(0, 47, 167);
public static Color hong = Color.FromRgb(255, 0, 0);
public static Color lv = Color.FromRgb(0, 255, 0);
public static Color lan = Color.FromRgb(0, 0, 255);
public static Color huang = Color.FromRgb(255, 255, 0);
public static Color yanghong = Color.FromRgb(255, 0, 255);
public static Color qing = Color.FromRgb(0, 255, 255);
public static Color bai = Color.FromRgb(255, 255, 255);
public static Color hei = Color.FromRgb(0, 0, 0);
}
}
视口包围盒
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IFoxDemo
{
public static class 视口包围盒
{
public static Extents3d GetViewExtents3d()
{
ViewTableRecord vtr = Env.Editor.GetCurrentView();//获取当前视图
double w = vtr.Width;//视图宽度
double h = vtr.Height;//视图高度
Point2d pc = vtr.CenterPoint;//视图中心点xy坐标
Point3d pmin = new Point3d(pc.X - w / 2, pc.Y - h / 2, 0);//当前视图左下角坐标
Point3d pmax = new Point3d(pc.X + w / 2, pc.Y + h / 2, 0);//当前视图右上角坐标
Extents3d ext = new Extents3d(pmin, pmax);//当前视图包围盒
ext.TransformBy(Matrix3d.Displacement(vtr.Target - Point3d.Origin));//从用户坐标系转为世界坐标系?ucsto wcs
return ext;
}
}
}