获取图块里的所有对象(包含子图块的对象)
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
namespace ForeachBlockAllObject
{
public class Class1
{
private static int intLayerNum;
public void foreachBlockAllObject()
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
using (DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
//定义过滤器
TypedValue[] filList = new TypedValue;
filList = new TypedValue(0, "Insert");
SelectionFilter filter = new SelectionFilter(filList);
//选择对象
PromptSelectionResult res = ed.GetSelection(filter);
if (res.Status == PromptStatus.OK)
{
intLayerNum = 1;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//遍历选择集
foreach (ObjectId id in res.Value.GetObjectIds())
{
//获取选择集里的图块
BlockReference blockObject = trans.GetObject(id, OpenMode.ForRead) as BlockReference;
ed.WriteMessage("***************************************************************\n");
//获取图块的块名称
ed.WriteMessage("第" + intLayerNum++.ToString("000") + "层 图块名称:" + blockObject.Name + "\n");
ed.WriteMessage("===============================================================\n");
//获取图块中的所有实体对象
getBlockSubEntity(ed, trans, blockObject);
ed.WriteMessage("***************************************************************\n");
}
trans.Commit();
}
}
}
}
/// <summary>
/// 获取图块中的所有实体对象
/// </summary>
/// <param name="ed"></param>
/// <param name="trans"></param>
/// <param name="blockObject"></param>
private static void getBlockSubEntity(Editor ed, Transaction trans, BlockReference blockObject)
{
//获取图块的块表记录
BlockTableRecord btrBlock = trans.GetObject(blockObject.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
//遍历图块里的所有实体对象
foreach (ObjectId BlcokId in btrBlock)
{
Entity BlockEntity = trans.GetObject(BlcokId, OpenMode.ForRead) as Entity;
if (BlockEntity is BlockReference)
{
//获取图块的块名称
ed.WriteMessage("第" + intLayerNum++.ToString("000") + "层 图块名称:" + ((BlockReference)BlockEntity).Name + "\n");
ed.WriteMessage("===============================================================\n");
//获取图块中的所有实体对象
getBlockSubEntity(ed, trans, (BlockReference)BlockEntity);
}
ed.WriteMessage("实体对象类型:" + BlockEntity.GetType().ToString() + "\n");
ed.WriteMessage("实体objectId:" + BlockEntity.ObjectId + "\n");
ed.WriteMessage("实体对象句柄:" + BlockEntity.Handle + "\n");
ed.WriteMessage("---------------------------------------------------------------\n");
}
ed.WriteMessage("===============================================================\n");
}
}
}
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的常规信息通过下列属性集
// 控制。更改这些属性值可修改
// 与程序集关联的信息。
// 将 ComVisible 设置为 false 使此程序集中的类型
// 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型,
// 则将该类型上的 ComVisible 属性设置为 true。
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
// 程序集的版本信息由下面四个值组成:
//
// 主版本
// 次版本
// 内部版本号
// 修订号
//
// 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值,
// 方法是按如下所示使用“*”:
//
页:
[1]