using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using DYH.Tools;
using IFoxCAD.Cad;
using System.Linq;
namespace DYH.Action
{
public class QuXianZongChang
{
private static double textHeight = 300;
/// <summary>
/// 曲线总长
/// </summary>
[CommandMethod("QXZC")]
public void QXZC()
{
using var tr = new DBTrans();
var pso = new PromptSelectionOptions() { MessageForAdding = "\n请选择所有要统计长度的曲线" };
var r0 = Env.Editor.GetSelection(pso);//提示用户选择
if (r0.Status == PromptStatus.OK)//如果用户正确选择
{
var entlist = r0.GetEntities<Entity>(tr, OpenMode.ForRead);//拿到所有对象
var curList = from ent in entlist where ent is Curve select (Curve)ent;//筛选出所有是曲线的对象
var length = curList.Select(x => x.GetLength()).Sum() / 1000;//求出总长度,因为获取的是毫米转为米要除以1000
var str = "总长为" + length.ToString("F2") + "m";//将长度保留两位小数,然后加上前后缀生成字符串\
Env.Editor.WriteMessage("\n" + str);//在命令栏生成字符串
}
}
}
}
//其中用到的其他函数
public static class PromptTool
{
public static List<T> GetEntities<T>(this PromptSelectionResult psr, Transaction trans, OpenMode openMode = OpenMode.ForRead)
{
var list = new List<T>();
if (psr.Status == PromptStatus.OK)
{
var ids = psr.Value.GetObjectIds();
foreach (var oid in ids)
{
var ent = trans.GetObject(oid, openMode);
if(ent is T ent1)
{
list.Add(ent1);
}
}
}
return list;
}
}