[CommandMethod("loop", CommandFlags.UsePickSet)]
public void 得非标孔()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
// 获取用户的选择
PromptSelectionResult psr = ed.SelectImplied();
if (psr.Status != PromptStatus.OK)
{
psr = ed.GetSelection();
}
if (psr.Status != PromptStatus.OK)
{
ed.WriteMessage("没有选中任何对象。\n");
return;
}
List<List<(List<(double, double)>, Entity)>> 线集 = new List<List<(List<(double, double)>, Entity)>>();
List<(List<(double, double)>, Entity)> 全部edge = new List<(List<(double, double)>, Entity)>();
SelectionSet sSet = psr.Value;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
foreach (SelectedObject selObj in sSet)
{
Entity ent = (Entity)trans.GetObject(selObj.ObjectId, OpenMode.ForWrite);
List<(double, double)> 点集 = new List<(double, double)>();
if (ent is Line)
{
var line = (Line)ent;
var 起点 = line.StartPoint;
var 终点 = line.EndPoint;
点集.Add((Math.Round(起点.X, 2), Math.Round(起点.Y, 2)));
点集.Add((Math.Round(终点.X, 2), Math.Round(终点.Y, 2)));
// Debug.WriteLine($"line:起点=({Math.Round(起点.X, 2)},{Math.Round(起点.Y, 2)})");
//Debug.WriteLine($"line:终点=({Math.Round(终点.X, 2)},{Math.Round(终点.Y, 2)})");
}
else if (ent is Arc)
{
var arc = (Arc)ent;
var 起点 = arc.StartPoint;
var 终点 = arc.EndPoint;
点集.Add((Math.Round(起点.X, 2), Math.Round(起点.Y, 2)));
点集.Add((Math.Round(终点.X, 2), Math.Round(终点.Y, 2)));
Debug.WriteLine($"arc:起点=({Math.Round(起点.X, 2)},{Math.Round(起点.Y, 2)})");
Debug.WriteLine($"arc:终点=({Math.Round(终点.X, 2)},{Math.Round(终点.Y, 2)})");
}
else continue;
全部edge.Add((点集, ent));
}
BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForWrite);
// 提交事务
var 已过边集 = new List<(List<(double, double)>, Entity)>();
foreach (var edge in 全部edge)
{
if (已过边集.Contains(edge)) continue;
var loop = new List<(List<(double, double)>, Entity)>();
loop.Add(edge);
var 点集 = edge.Item1;
var ent = edge.Item2;
var 循环=true;
while (循环) {
var 还有可接线段 = false;
//每条边都过一遍,看能不能接上;
foreach (var 边 in 全部edge)
{
if (loop.Contains(边)) continue;
点集.Any(点 =>
{
if (边.Item1.Any(item => item == 点))
{
点集.AddRange(边.Item1.Where(item => item != 点));
loop.Add(边);
已过边集.Add(边);
还有可接线段 = true;
}
return 还有可接线段;
});
}
if (!还有可接线段) 循环 = false;
};
线集.Add(loop);
}
Debug.WriteLine("loop数=" + 线集.Count);
int i = 0;
foreach (var loop in 线集)
{
Debug.WriteLine("edge数="+ loop.Count);
i++;
BlockTableRecord btr = new BlockTableRecord();
btr.Name = "cimcad" + i;
foreach (var edge in loop)
{
int colorindex = 0;
Math.DivRem(i, 7, out colorindex);
if (colorindex == 0) colorindex++;
edge.Item2.ColorIndex = colorindex;
// btr.AppendEntity(edge.Item2);
// bt.Add(btr);
// trans.AddNewlyCreatedDBObject(btr, true);
}
}
trans.Commit();
}
}
cad c# 获得loop
最新推荐文章于 2024-09-09 15:17:40 发布