当前位置: 首页 > article >正文

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;
 }


http://www.kler.cn/a/379223.html

相关文章:

  • Linux(inode + 软硬链接 图片+大白话)
  • 「Mac畅玩鸿蒙与硬件15」鸿蒙UI组件篇5 - Slider 和 Progress 组件
  • C# 日志框架 NLog、log4net 和 Serilog对比
  • dc源码铺子应用部署教程
  • Android亮屏Job的功耗优化方案
  • Spring1(初始Spring 解耦实现 SpringIOC SpringDI Spring常见面试题)
  • (57)MATLAB使用迫零均衡器和MMSE均衡器的BPSK调制系统仿真
  • 用图说明 CPU、MCU、MPU、SoC 的区别
  • Chromium127编译指南 Linux篇 - 同步第三方库以及Hooks(六)
  • blender导入的图片渲染看不见,图片预览正常,但渲染不出
  • 什么影响网站的SEO排名
  • 【Excel】常用公式记录
  • Dubbo 构建高效分布式服务架构
  • 项目推荐:指针切换器
  • 骨传导耳机品牌排行榜前十名推荐!五款王牌爆款机型推荐!
  • warmup
  • (自用复习题)常微分方程08
  • 手游和应用出海资讯:可灵AI独立APP即将上架;Rollic在英国推出芭比合并解谜手游
  • UE Mutiplayer(1):网络概述
  • SpringBoot音乐天地:构建Web音乐网站
  • sublime python出现中文乱码怎么办
  • 文件上传和文件包含
  • 国家电网某话务中心IT基础监控应用案例
  • Vue3.js - 计算属性监视属性
  • Linux运行Java程序,并按天输出日志
  • 数论——约数