使用工具==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;
}