本帖最后由 suzhiyu 于 2011-8-4 15:36 编辑
继续在论坛翻找资料,根据http://bbs.mjtd.com/thread-78973-1-1.html【[符号表] [原创]翔麟专集——获取图块里的所有对象(包含子图块的对象)】实现了块数据的分解,重构了getBlockSubEntity函数
-----------------------------------------------------------
目前对编组group这个功能还是不够了解
- /// <summary>
- /// 遍历一个DWG文件的块定义
- /// </summary>
- [Autodesk.AutoCAD.Runtime.CommandMethod("ListBlockDef")]
- public void ListBlockDef()
- {
- try
- {
- Database acDatabase = Application.DocumentManager.MdiActiveDocument.Database;
- using (Transaction trans = acDatabase.TransactionManager.StartTransaction())
- {
- BlockTable acBlkTable = trans.GetObject(acDatabase.BlockTableId, OpenMode.ForRead) as BlockTable;
- StringBuilder strBuilder = new StringBuilder();
- foreach (ObjectId id in acBlkTable)
- {
- SymbolTableRecord symTableRec = trans.GetObject(id, OpenMode.ForRead) as SymbolTableRecord;
- string str = symTableRec.GetType().ToString();
- strBuilder.Append(string.Format("{0}-{1} \n", symTableRec.Name, str));
- str = getBlockSubEntity(trans, symTableRec as BlockTableRecord);
- strBuilder.Append(str);
- }
- Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(strBuilder.ToString());
- }
- }
- catch (System.Exception ex)
- {
- Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.Message);
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="trans"></param>
- /// <param name="btrBlock"></param>
- /// <returns></returns>
- private string getBlockSubEntity(Transaction trans, BlockTableRecord btrBlock)
- {
- try
- {
- StringBuilder strBuilder = new StringBuilder();
- //遍历图块里的所有实体对象
- foreach (ObjectId BlcokId in btrBlock)
- {
- Entity BlockEntity = trans.GetObject(BlcokId, OpenMode.ForRead) as Entity;
- if (BlockEntity is BlockReference)
- {
- //获取图块的块名称
- BlockReference blockObject = BlockEntity as BlockReference;
- strBuilder.Append(blockObject.Name + "\n\n");
- //获取图块中的所有实体对象
- BlockTableRecord btrBlockSub = trans.GetObject(blockObject.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
- string str = getBlockSubEntity(trans, btrBlockSub);
- strBuilder.Append(str);
- }
- strBuilder.Append("实体对象类型:" + BlockEntity.GetType().ToString() + "\n");
- strBuilder.Append("实体objectId:" + BlockEntity.ObjectId + "\n");
- strBuilder.Append("实体对象句柄:" + BlockEntity.Handle + "\n");
- strBuilder.Append("\n");
- }
- strBuilder.Append("===============================================================\n");
- return strBuilder.ToString();
- }
- catch (System.Exception ex)
- {
- return ex.Message;
- }
- }
|