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;

        [Autodesk.AutoCAD.Runtime.CommandMethod("GetBlockObject")]
        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[1];
                filList[0] = 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");
        }
    }
}