using Autodesk.AutoCAD.ApplicationServices;
using Acap = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using IFoxCAD.Cad;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Windows;
using System.Runtime.InteropServices;
namespace DYH.Demo
{
public class ShiQuXianChang
{
//目标,选择一条线,点击文字,将线的长度赋值到文字上;
[CommandMethod("SQXC")]
public void SQXC()
{
//建事务
using var tr = new DBTrans();
//选择对象
var r1 = Env.Editor.GetEntity("\n选择一条线");
if (r1.Status == PromptStatus.OK)//如果选择到了对象
{
var ent1 = tr.GetObject<Entity>(r1.ObjectId);//获取对象实体
if (ent1 is Curve cur)//如果对象是线
{
var length = cur.GetLength() / 1000;//获取长度;获取到的是毫米,转为m除以1000;
var r2 = Env.Editor.GetEntity("\n选择要赋值的文字");//提示用户选择文字
if (r2.Status == PromptStatus.OK)//如果用户选到对象
{
var ent2 = tr.GetObject<Entity>(r2.ObjectId, OpenMode.ForWrite);//拿到对象;因为要赋值所以为ForWrite可写模式
if (ent2 is DBText text)//如果选到的对象是文字
{
text.TextString = "长度为:" + length.ToString("0.00") + "m";//按格式赋值并保留两位小数
}
}
}
}
}
}
}