- 积分
- 6549
- 明经币
- 个
- 注册时间
- 2016-10-12
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 wang2006zhi 于 2025-2-14 12:03 编辑
 - [CommandMethod("tt2")]
- public void Tt2()
- {
- using var tr = new DBTrans();
- var fil = OpFilter.Build(x => x.Dxf(0) == "LINE");
- if (!Env.Editor.SelEnts(out List<Line> lines, fil: fil))
- return;
- var treeRoot = new QuadTree<CadEntity>(AppX.InfoRect);
- foreach (var line in lines)
- {
- if (line is null)
- return;
- var cadEnt = new CadEntity(line, line.GeometricExtents.GetRect());
- treeRoot.Insert(cadEnt);
- }
- while (true)
- {
- var sw = Stopwatch.StartNew();
- if (!Env.Editor.SelEnt(out Line line))
- return;
- var cadEnt = new CadEntity(line, line.GeometricExtents.GetRect());
- var linkIds = new List<Line>();
- DfS(cadEnt, ref linkIds);
- if (!linkIds.Any())
- continue;
- linkIds.ForEach(ent =>
- {
- using (ent.ForWrite())
- {
- ent!.ColorIndex = ent.ColorIndex == 1 ? 2 : 1;
- ent.Draw();
- }
- });
- sw.Stop();
- Env.Editor.WriteMessage("\n TT2用时{0}毫秒", sw.Elapsed.TotalMilliseconds);
- }
- void DfS(CadEntity cadEnt, ref List<Line> linkIds)
- {
- var cadEnts = treeRoot.Query(cadEnt.Expand(0));
- if (!cadEnts.Any())
- return;
- foreach (CadEntity item in cadEnts)
- {
- if (cadEnt.Equals(item))
- continue;
- var lineA = (Line)cadEnt.Entity;
- var lineB = (Line)item.Entity;
- if (linkIds.Contains(lineB))
- continue;
- if (IsLink(lineA, lineB))
- {
- linkIds.Add(lineB);
- DfS(item, ref linkIds);
- }
- }
- }
- bool IsLink(Line lineA, Line lineB)
- {
- var tol = new Tolerance(5, 50);
- if (lineA.StartPoint.IsEqualTo(lineB.StartPoint, tol)
- || lineA.StartPoint.IsEqualTo(lineB.EndPoint, tol)
- || lineA.EndPoint.IsEqualTo(lineB.StartPoint, tol)
- || lineA.EndPoint.IsEqualTo(lineB.EndPoint, tol)
- )
- return true;
- return false;
- }
- }
- private class CadEntity(Entity ent, Rect box) : QuadEntity(box)
- {
- public readonly Entity Entity = ent;
- //这里加入其他字段
- public int CompareTo(CadEntity? other)
- {
- if (other == null)
- return -1;
- return GetHashCode() ^ other.GetHashCode();
- }
- public override int GetHashCode()
- {
- return (base.GetHashCode(), Entity.GetHashCode()).GetHashCode();
- }
- }
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
|