Select Entity in BlockReference

Select Entity in BlockReference

SimonaQQ
Advocate Advocate
926 Views
5 Replies
Message 1 of 6

Select Entity in BlockReference

SimonaQQ
Advocate
Advocate

Hello,

I want the user to select a certain entity within a block reference, similar to the selection functionality in the 'NCopy' command. Does the API support this operation?

I tried using the ed.GetSelection(selOpts) method and modified the AllowSubSelections property, but it didn’t work. What should I do?

0 Likes
Accepted solutions (1)
927 Views
5 Replies
Replies (5)
Message 2 of 6

_gile
Consultant
Consultant
Accepted solution

Hi,

Have a look at the Editor.GetNestedEntity method.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 6

sapch
Advocate
Advocate

I’ve found that when a block contains Advance Steel elements, the CAD elements and AS elements become out of sync, for example during matrix transformations such as translation or alignment. Could you advise how to handle this? Or have you run into similar issues?

0 Likes
Message 4 of 6

ActivistInvestor
Mentor
Mentor

Try describing your problem in a bit greater detail. For example, what does 'out of sync' mean? Are you using the transformation matrix provided by the PromptNestedEntityResult? A code snippet or two might also help.

0 Likes
Message 5 of 6

sapch
Advocate
Advocate

[AAR.CommandMethod("EXPLODEAS", AAR.CommandFlags.Modal)]
public void CloneBlockEntitiesToPoint()
{
AAA.Document doc = AAA.Application.DocumentManager.MdiActiveDocument;
AAE.Editor ed = doc.Editor;
AAD.Database db = doc.Database;

try
{
// 1. 用户输入块名
AAE.PromptStringOptions pso = new AAE.PromptStringOptions("\n请输入要提取元素的块名称:");
AAE.PromptResult psr = ed.GetString(pso);
if (psr.Status != AAE.PromptStatus.OK || string.IsNullOrWhiteSpace(psr.StringResult))
{
ed.WriteMessage("\n未输入块名");
return;
}
string blockName = psr.StringResult.Trim();

// 2. 用户指定目标位置
AAE.PromptPointOptions ppo = new AAE.PromptPointOptions("\n请指定元素放置的目标位置:");
AAE.PromptPointResult ppr = ed.GetPoint(ppo);
if (ppr.Status != AAE.PromptStatus.OK)
{
ed.WriteMessage("\n未指定位置");
return;
}
AAG.Point3d targetPos = ppr.Value;

using (AAD.Transaction tr = db.TransactionManager.StartTransaction())
{
AAD.BlockTable blockTable = tr.GetObject(db.BlockTableId, AAD.OpenMode.ForRead) as AAD.BlockTable;

// 判断块是否存在
if (!blockTable.Has(blockName))
{
ed.WriteMessage($"\n❌ 块不存在:{blockName}");
tr.Commit();
return;
}

// 打开块定义
AAD.BlockTableRecord blockDef = tr.GetObject(blockTable[blockName], AAD.OpenMode.ForRead) as AAD.BlockTableRecord;
AAD.BlockTableRecord currentSpace = tr.GetObject(db.CurrentSpaceId, AAD.OpenMode.ForWrite) as AAD.BlockTableRecord;

// ==============================================
// 构建目标坐标系矩阵(你原来的对齐逻辑)
// ==============================================
AAG.Matrix3d targetMatrix = AAG.Matrix3d.Displacement(targetPos - AAG.Point3d.Origin);

// ==============================================
// 直接克隆块内所有元素 → 放到目标位置
// ==============================================
foreach (AAD.ObjectId entId in blockDef)
{
AAD.Entity ent = tr.GetObject(entId, AAD.OpenMode.ForRead) as AAD.Entity;
if (ent == null) continue;

// 深度克隆
AAD.Entity cloneEnt = ent.Clone() as AAD.Entity;
if (cloneEnt == null) continue;

if (cloneEnt.GetRXClass().DxfName == "ASTPLATE")
{

targetMatrix = AAG.Matrix3d.AlignCoordinateSystem(AAG.Point3d.Origin, AAG.Vector3d.XAxis, AAG.Vector3d.YAxis, AAG.Vector3d.ZAxis, targetPos, AAG.Vector3d.XAxis, AAG.Vector3d.YAxis, AAG.Vector3d.ZAxis);
//MessageBox.Show("1");
M_BIM.ShowXYZ(" 目标点 ", M_BIM.Ch_Point(targetPos));

cloneEnt.TransformBy(targetMatrix);

}
else
{
// 应用你原来的对齐变换 + 目标位置
cloneEnt.TransformBy(targetMatrix);

}

cloneEnt.ColorIndex = 1; // 红色

// 添加到图纸
currentSpace.AppendEntity(cloneEnt);
tr.AddNewlyCreatedDBObject(cloneEnt, true);
}

tr.Commit();
ed.WriteMessage($"\n✅ 成功!块【{blockName}】内所有元素已克隆并放置到指定位置!");
}
}
catch (System.Exception ex)
{
ed.WriteMessage($"\n❌ 错误:{ex.Message}");
}
}

0 Likes
Message 6 of 6

sapch
Advocate
Advocate

sapch_0-1776046665087.png

 

0 Likes