获取块内实体 封装

using System;

using System.Collections.Generic;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Geometry;

namespace CadTools

{

/// <summary>

/// 嵌套实体拾取结果

/// </summary>

public class NestedPickResult

{

/// <summary>

/// 最终命中的实体ID

/// </summary>

public ObjectId EntityId;

/// <summary>

/// 最终命中的实体

/// </summary>

public Entity Entity;

/// <summary>

/// 从外到内的块路径

/// </summary>

public List<ObjectId> BlockPath = new List<ObjectId>();

/// <summary>

/// 累计变换矩阵

/// </summary>

public Matrix3d Transform = Matrix3d.Identity;

}

public static class NestedEntityHelper

{

/// <summary>

/// 按点获取最深层嵌套实体

/// </summary>

/// <param name="db">数据库</param>

/// <param name="pt">世界坐标点</param>

/// <param name="tol">命中容差</param>

/// <returns></returns>

public static NestedPickResult 按点获取最深层嵌套实体(

this Database db,

Point3d pt,

double tol = 1.0)

{

if (db == null)

return null;

NestedPickResult result = null;

using (Transaction tr = db.TransactionManager.StartTransaction())

{

BlockTable bt =

(BlockTable)tr.GetObject(

db.BlockTableId,

OpenMode.ForRead);

BlockTableRecord ms =

(BlockTableRecord)tr.GetObject(

btBlockTableRecord.ModelSpace,

OpenMode.ForRead);

FindInBlock(

tr,

ms,

pt,

Matrix3d.Identity,

new List<ObjectId>(),

tol,

ref result);

tr.Commit();

}

return result;

}

/// <summary>

/// 递归扫描块

/// </summary>

private static bool FindInBlock(

Transaction tr,

BlockTableRecord btr,

Point3d worldPt,

Matrix3d currentTransform,

List<ObjectId> path,

double tol,

ref NestedPickResult result)

{

bool found = false;

foreach (ObjectId id in btr)

{

Entity ent =

tr.GetObject(id, OpenMode.ForRead) as Entity;

if (ent == null)

continue;

// 先处理块参照

BlockReference br = ent as BlockReference;

if (br != null)

{

Matrix3d newTransform =

currentTransform * br.BlockTransform;

BlockTableRecord childBtr;

try

{

ObjectId btrId =

br.IsDynamicBlock

? br.DynamicBlockTableRecord

: br.BlockTableRecord;

childBtr =

(BlockTableRecord)tr.GetObject(

btrId,

OpenMode.ForRead);

}

catch

{

continue;

}

if (!PointInsideEntityExtents(

br,

worldPt,

currentTransform,

tol))

{

continue;

}

List<ObjectId> newPath =

new List<ObjectId>(path);

newPath.Add(br.ObjectId);

if (FindInBlock(

tr,

childBtr,

worldPt,

newTransform,

newPath,

tol,

ref result))

{

found = true;

}

}

// 当前实体命中检测

if (EntityHitTest(

ent,

worldPt,

currentTransform,

tol))

{

result = new NestedPickResult();

result.EntityId = ent.ObjectId;

result.Entity = ent;

result.BlockPath =

new List<ObjectId>(path);

result.Transform =

currentTransform;

found = true;

}

}

return found;

}

/// <summary>

/// 实体命中测试

/// </summary>

private static bool EntityHitTest(

Entity ent,

Point3d worldPt,

Matrix3d transform,

double tol)

{

try

{

if (!PointInsideEntityExtents(

ent,

worldPt,

transform,

tol))

{

return false;

}

Curve curve = ent as Curve;

if (curve != null)

{

Curve clone =

curve.GetTransformedCopy(transform)

as Curve;

Point3d closest =

clone.GetClosestPointTo(

worldPt,

false);

double dist =

closest.DistanceTo(worldPt);

clone.Dispose();

return dist <= tol;

}

return true;

}

catch

{

return false;

}

}

/// <summary>

/// 包围盒快速过滤

/// </summary>

private static bool PointInsideEntityExtents(

Entity ent,

Point3d pt,

Matrix3d transform,

double tol)

{

try

{

Extents3d ext = ent.GeometricExtents;

ext.TransformBy(transform);

return

pt.X >= ext.MinPoint.X - tol &&

pt.X <= ext.MaxPoint.X + tol &&

pt.Y >= ext.MinPoint.Y - tol &&

pt.Y <= ext.MaxPoint.Y + tol &&

pt.Z >= ext.MinPoint.Z - tol &&

pt.Z <= ext.MaxPoint.Z + tol;

}

catch

{

return false;

}

}

}

}

相关推荐
笨蛋©2 天前
2026年制造业数字化:如何通过 Infra CONVERT 中国代理 实现检验计划自动化
ai·数字化·cad·质量管理·制造业
笨蛋©5 天前
2026年高效生成CAD图纸气泡图与自动化检验计划的技术路径
ai·数字化·cad·质量管理·制造业
小幂同学5 天前
CAD图纸怎么转PDF和图片?CAD快速看图王(幂果)转换方法解析
pdf·cad
广州智造5 天前
HyperMesh 产品功能清单|有限元前处理软件|HyperMesh 中国代理
人工智能·教程·设计·cad·建模·cae
cuicuiniu5216 天前
3D图纸交付格式不对怎么办?如何快速进行3D格式转换?
3d·cad·cad看图·cad看图王
笨蛋©9 天前
[实战] 2026年制造业数字化:工程图纸GD&T识别与检验计划自动化指南
ai·cad·质量管理·制造业·图纸识别
北漂燕郊杨哥9 天前
CAD Mini Viewer —— 一个纯 Go 编写的轻量 DXF 查看器
golang·cad·dxf·cadview
cuicuiniu52112 天前
三维/3D格式转换用什么软件好?
cad·cad看图·cad看图软件·cad看图王·浩辰cad看图王
cuicuiniu52120 天前
CAD图纸分割保存技巧,大图自由拆分导出DWG/PDF
cad·cad看图·cad看图软件·cad看图王·浩辰cad看图王
我才是银古20 天前
方向性形态学开运算:用 Clipper 拆解不规则多边形
cad·布尔运算·clipper