019集——获取CAD图中多个实体的包围盒(CAD—C#二次开发入门)
如下图所示,获取多个实体的最大包围盒,用红色线表示:
也可单独选圆的包围盒
部分代码如下:
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 = NetOffice.ExcelApi;
namespace AcTools
{
public class Class1
{
#region
//Polyline pl = new Polyline(3);
//pl.AddVertexAt(0, new Point2d(10, 2), 0, 0, 0); // 起点
//pl.AddVertexAt(1, new Point2d(30, 5), 0, 0, 0); // 起点
//pl.AddVertexAt(2, new Point2d(50, 25), 0, 0, 0); // 起点
//Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
//db.AddEntityToModeSpace(line);
#endregion
[CommandMethod("xx")]
public void XX()
{ var db = Z.db;
/// 包围盒
List<Entity> ents = db.SelectEntities<Entity>();
List<double> lis = Getboxs(ents);
Polyline pl = new Polyline();
if (lis.Count == 0 )
{
Z.ed.WriteMessage("未选择对象!");
return;
}
pl.AddVertexAt(0, new Point2d(lis[0], lis[1]),0,0,0);
pl.AddVertexAt(1, new Point2d(lis[2], lis[1]), 0, 0, 0);
pl.AddVertexAt(2, new Point2d(lis[2], lis[3]), 0, 0, 0);
pl.AddVertexAt(3, new Point2d(lis[0], lis[3]), 0, 0, 0);
pl.Closed = true;
pl.ColorIndex = 1;
db.AddEntityToModeSpace(pl);
List< Point2d> pt = new List<Point2d> {
new Point2d(lis[0]+1000, lis[1]+1000),
new Point2d(lis[2]+1000, lis[1] + 1000),
new Point2d(lis[2]+1000, lis[3] + 1000),
new Point2d(lis[0]+1000, lis[3]+1000)
};
db.AddPolyLineToModeSpace(true, 0, pt.ToArray());
db.Zoom();
//var ed = Z.ed;
#region
// 声明数据库对象
//Database db = HostApplicationServices.WorkingDatabase;
//db.EraseAll();
// db.AddTable(6,6);
//db.Zoom();
//List<Entity> ents = new List<Entity>();
//for (int i = 0; i < 5; i++)
//{
// Line line = new Line(Point3d.Origin, new Point3d(100 * i, 100, 0));
// ents.Add(line);
//}
//ObjectId oid = db.AddAttBlock("属性块1", ents, new Point3d(0, 100, 0), "图号", "图号内容:", "2024-100", 20);
ObjectId oid = db.AddAttBlock("属性块2", ents,new Point3d(100,100,0),"tag2","提示","显示文本",10);
ObjectId bid = db.InsertAttrBlock(oid, Point3d.Origin, 0, 1, 1, 1);
//Dictionary<string,string> dic= new Dictionary<string,string> ();
// dic.Add("tag2","修改后的值3");
//bid.ChangeBlockAttr(dic);
db.ChangeBlockColor(oid, 5);
ObjectId bref = db.InsertAttrBlock(oid, new Point3d(200, 0, 0), 0, 1, 1, 1);
oid.EraseBlock();
bref.EraseEntity();
// db.DataToTableDemo();
//db.Zoom();
db.Savefd();
db.EraseBlockSelectonScreen();
BlockData blockData = new BlockData();
//db.TxtToDwg();
//SaveFileDialog savefd;
//DialogResult saveDlgRes;
//db.Sfd(out savefd, out saveDlgRes);
//if (saveDlgRes == Wform.DialogResult.OK)
//{
// BlockData[] data = new BlockData[2];
// data[0].layerName = "0的图层名";
// data[1].blockName = "1的块名";
// string[] contents = new string[data.Length];
// for (int i = 0; i < data.Length; i++)
// {
// contents[i] = data[i].blockName + data[i].layerName + ",";
// }
// //string[] contents = new string[] { "1111", "dzb" };
// File.WriteAllLines(savefd.FileName, contents);
// db.DwgToTxt();
//db.TxtToDwg();
//OpenFileDialog ofd = new OpenFileDialog();
//DialogResult ofdr = ofd.ShowDialog();
//db.Ofd(out ofd, out ofdr );
//SaveFileDialog sfd;//= new SaveFileDialog();
//DialogResult sfdr;//` = sfd.ShowDialog();
//db.Sfd(out sfd, out sfdr);
//db.DwgToTxt();
//db.TxtToDwg();
//Excel.Application excelAPP = new Excel.Application();
//Excel.Workbook book = excelAPP.Workbooks.Add() ;
//Excel.Worksheet sheet = (Excel.Worksheet) book.Worksheets[0] ;
// sheet.Cells["A1"].Value = "dzb";
//excelAPP.Visible = true ;
//excelAPP.Worksheets.Add(book) ;
//excelAPP.Worksheets.Add(book);
#endregion
//db.DDwgToTxt();
// db.TxtToDwg();
//ed.WriteMessage("1");
}
private List<double> Getboxs(List<Entity> entities)
{
List< double> lis = new List< double>();
if (entities.Count ==0)
{
return lis;
}
double minx = entities.Min(x => x.Bounds.Value.MinPoint.X);
double miny = entities.Min(x => x.Bounds.Value.MinPoint.Y);
double maxx = entities.Max(x => x.Bounds.Value.MaxPoint.X);
double maxy = entities.Max(x => x.Bounds.Value.MaxPoint.Y);
lis.Add(minx);
lis.Add(miny);
lis.Add(maxx);
lis.Add(maxy);
return lis;
}
}
}
public static List<T> SelectEntities<T>(this Database db) where T : Entity
{
List<T> result = new List<T>();
Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
PromptSelectionResult psr = editor.GetSelection();
if (psr.Status == PromptStatus.OK)
{
ObjectId[] objectids = psr.Value.GetObjectIds();
Database database = HostApplicationServices.WorkingDatabase;
using (Transaction tran = database.TransactionManager.StartTransaction())
{
foreach (var item in objectids)
{
Entity entity = item.GetObject(OpenMode.ForRead) as Entity;
if (entity is T)
{
result.Add(entity as T);
}
}
}
}
return result;
}