[CommandMethod("BlockPreview")]
public void GenerateBlockPreview()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptFileNameResult result = ed.GetFileNameForOpen("请选择需要预览的文件");
if (result.Status != PromptStatus.OK) return;
string filename = result.StringResult;
//在C盘跟目录线,创建一个临时文件夹,用于存放文件中的块预览图标
string path = "C:\\Temp1";
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
using (Transaction tran = db.TransactionManager.StartTransaction())
{
//导入外部的块
db.ImportBlocksFrmDwg(filename);
//打开块表
BlockTable bt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
foreach (ObjectId id in bt)
{
BlockTableRecord btr = tran.GetObject(id, OpenMode.ForRead) as BlockTableRecord;
//如果是匿名块,布局块以及没有预览图型块,则返回
if (btr.IsAnonymous || btr.IsLayout || !btr.HasPreviewIcon)
{
//获取预览图块,适用于2008及以下版本
Bitmap preview = BlockThumbnailHelper.GetBlockThumbanail(btr.ObjectId);
//
//Bitmap preview = btr.PreviewIcon;//适用于2009版本及以上版本
preview.Save(path + "\\" + btr.Name + ".bmp");//保存块预览
}
}
tran.Commit();
}
}
[本日志由 tiancao1001 于 2021-03-02 02:28 PM 编辑]
暂时没有评论