如何访问块定义和成组数据?
本帖最后由 suzhiyu 于 2011-8-4 15:08 编辑块定义数据也是保存在BlockTable中吗, 怎么读取?如果复杂的块是点、线、面和文字混合组成,如何分解为单项?同样的如果是成组group的数据,怎么读取和分解?
在《AutoCAD .NET 开发人员手册》中这些内容我没有看到,高手出来指点下,或者在ObjectARX有解答也可以。
----------------------------------------------------------------------------------------------------------
在论坛找了好久,根据下面这个帖子:
http://bbs.mjtd.com/forum.php?mod=viewthread&tid=84543&highlight=%CD%BC%BF%E9
实现了BlockTable中的块定义名称遍历,后面有新的突破我会继续更新。
/// <summary>
/// 遍历一个DWG文件的块定义
/// </summary>
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;
strBuilder.Append(string.Format("{0} \n", symTableRec.Name));
}
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(strBuilder.ToString());
}
}
catch (System.Exception ex)
{
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.Message);
}
}
本帖最后由 suzhiyu 于 2011-8-4 15:36 编辑
继续在论坛翻找资料,根据http://bbs.mjtd.com/thread-78973-1-1.html【[符号表] [原创]翔麟专集——获取图块里的所有对象(包含子图块的对象)】实现了块数据的分解,重构了getBlockSubEntity函数
-----------------------------------------------------------
目前对编组group这个功能还是不够了解
/// <summary>
/// 遍历一个DWG文件的块定义
/// </summary>
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;
}
}
编组的代码情形可以看:
http://bbs.mjtd.com/forum.php?mod=viewthread&tid=78963&star=1#37398
http://bbs.mjtd.com/thread-76123-1-1.html
在论坛翻找了4-5个小时,对雪山飞狐_lzh的仰慕之情溢于言表、受益良多 建议LZ自己写一遍,你就理解了...
页:
[1]