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

022集——统计多条线的总长度(CAD—C#二次开发入门)

如下图所示,选择多条线并统计长度:

c#中不包含直接获取curve曲线长度 属性,需用如下方法:curve.GetDistanceAtParameter(item.EndParam)

附部分代码如下:

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() 
        {
            double sum = 0;
           
            Z.db.GetEntities1(out List<Curve> curves, "\n请选择需要计算总长度的线:\n");
            if (curves == null|curves.Count==0) return ;
            Point3d ps = Getcenterpoint(curves);
            foreach (var item in curves)
            {
                sum = sum + item.GetDistanceAtParameter(item.EndParam);
                
            }
            //double sum3 = curves.Sum(x => x.GetDistanceAtParameter(x.EndParam));
            sum = Math.Round(sum, 3);
            //string sum2 = sum.ToString("f2");
            DBText text = new DBText() { Height = 100, Position =ps,TextString = $"总长度:{sum}m",ColorIndex= 1 };
            //DBText text2 = new DBText() { Position = ps, TextString = $"2:{sum2}㎡", ColorIndex = 2 };
            //DBText text3 = new DBText() { Position = ps, TextString = $"3:{sum3}㎡", ColorIndex = 3 };
            Z.db.AddEntityToModeSpace(text);//(text,text2,text3);
            Z.db.Zoom();
        }


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

        private Point3d Getcenterpoint<T>(List<T> entities) where T : Entity
        {
           
            if (entities.Count == 0)
            {
                return Point3d.Origin;
            }
            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);
            Point3d point = new Point3d((minx+maxx)/2,(miny+maxy)/2,0);
            return point;
        }


    }
}

 代码包含部分本博自己封装函数,CAD二次开发、插件定制↓↓↓


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

相关文章:

  • Virtuoso使用layout绘制版图、使用Calibre验证DRC和LVS
  • [前端面试]计算机网络
  • 记一次:使用使用Dbeaver连接Clickhouse
  • 转移概率矩阵的计算
  • 论文提交步骤 | 2024年第五届MathorCup大数据竞赛
  • log4cplus使用
  • 玄机-应急响应- Linux入侵排查
  • 人工智能进程;算子加速的具体计算部分;大模型GPT5:参数18万亿;大模型面临问题
  • Javaweb梳理5——约束
  • 第十四章大数据和数据科学
  • ✨云桥计划✨
  • Netty核心源码与优化
  • 微信公众号(或微信浏览器)获取openId(网页授权)
  • DAY15|二叉树Part03|LeetCode: 222.完全二叉树的节点个数、110.平衡二叉树、257. 二叉树的所有路径、404.左叶子之和
  • 江协科技STM32学习- P26 UART串口外设
  • 普通人的核心竞争力
  • 《Python游戏编程入门》注-第4章2
  • Unity XR Interaction Toolkit 开发教程(1):OpenXR 与 XRI 概述【3.0 以上版本】
  • 第13次CCF CSP认证真题解
  • 【GO学习笔记 go基础】访问控制
  • 我谈Canny算子
  • leetcode双指针题目总结
  • SAP B1 功能模块字段介绍 - 价格清单(上)
  • Flutter动画渐变
  • Java面试经典 150 题.P169. 多数元素(005)
  • java.sql.SQLException: ORA-00971: 缺失 SET 关键字