获取块内实体 封装

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;

}

}

}

}

相关推荐
笨蛋©8 小时前
基于Infra CONVERT 正版授权的图纸识别与FAI自动化实务
ai·数字化·cad·制造业·图纸识别
笨蛋©5 天前
[实战] 2026年制造业数字化:图片格式图纸识别与质量检验计划自动化
ai·cad·质量管理·制造业·图纸识别
笨蛋©6 天前
[技术深挖] 2026年CAD图纸气泡图数字化处理规范与FAI检验计划实务
ai·数字化·cad·质量管理·制造业
笨蛋©9 天前
2026年质量管理指南:泡泡图(Bubble Drawing)与自动化检验计划实战
ai·数字化·cad·质量管理·制造业
cuicuiniu52111 天前
.dwt是什么格式?用什么软件打开?
cad·cad看图·cad看图软件·cad看图王
笨蛋©13 天前
[实战] 2026年CNC加工质量管理:从数字化图纸识别到自动化检验计划(FAI)全流程
ai·数字化·cad·质量管理·图纸识别
cuicuiniu52118 天前
SKP查看常见问题与技巧:版本兼容、卡顿、乱码、分享
cad·cad看图·cad看图软件·cad看图王
笨蛋©20 天前
2026年工业数字化:解析Infra CONVERT德国标准下的工程图纸自动化处理与质量管理
ai·cad·质量管理·制造业·图纸识别