CAD二开(c#)小试牛刀系列6 用过滤集选择矩形并求出所有面积之和,用单行文字表示出来

使用工具==CAD2019+VS2022

主要作用==在CAD中同一个图层下有多种相互重叠的实体,只需要求出其中一种实体的面积(以圆与矩形为例,只求出矩形面积)

效果图片

代码如下

//主要代码
    Database db =HostApplicationServices.WorkingDatabase; 
    Editor ed = acadApp.Application.DocumentManager.MdiActiveDocument.Editor;    
    List<double> plAre = new List<double>();//面积
    int nums = 0;
    //指定图层与线型
    TypedValue[] types = new TypedValue[2];
    types[0] = new TypedValue((int)DxfCode.LayerName, "图层1");
    types[1] = new TypedValue((int)DxfCode.Start, "LWPOLYLINE");
    SelectionFilter filter = new SelectionFilter(types);
    PromptSelectionResult psResult = ed.GetSelection(filter);
    if (psResult.Status == PromptStatus.OK)
    {
        ObjectId[] ids = psResult.Value.GetObjectIds();
        using (Transaction trans = db.TransactionManager.StartTransaction())
        {
            foreach (var item in ids)
            {                
                Entity ents = trans.GetObject(item, OpenMode.ForRead) as Entity;
                if (ents is Polyline)
                {
                    Polyline pline = ents as Polyline;                            
                    plAre.Add(pline.Area);
                    nums = plAre.Count;
                }
            }
        }

    }
    //单行文字
    PromptPointOptions ppo = new PromptPointOptions("指点定位置");
    PromptPointResult ppr = ed.GetPoint(ppo);
    DBText text = new DBText();
    text.Position = new Point3d(ppr.Value.X, ppr.Value.Y, 0);
    text.TextString = "\n" + "共选择了" + nums.ToString() + "个," + "总面积为:" + CalculateSum(plAre).ToString("0.00");
    Tools.AddToModelSpace(db, text);            

// 求和函数
public static double CalculateSum(List<double> lianLenth)
{
    double sums = 0;
    for (int i = 0; i <lianLenth.Count ; i++)
    {
        sums = sums + lianLenth[i];
    }
    return sums;
}





 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值