我来写研究成果了。下面的代码可以实现清晰缩略图,但是有个问题:在ZCAD中,缩略图不显示块的属性,但是在ACAD中是可以显示的。
代码在ZCAD和ACAD都测试通过了。
Imports zwsoft.zwcad.ApplicationServices
Imports zwsoft.zwcad.DatabaseServices
Imports zwsoft.zwcad.EditorInput
Imports zwsoft.zwcad.Runtime
Imports zwsoft.zwcad.Geometry
Imports System.IO
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Color
Imports System.Reflection
Imports System.Drawing.Drawing2D
Imports System.Drawing.Bitmap
Imports zwsoft.zwcad.Windows
Imports zwsoft.zwcad.Windows.Data
Imports System.Windows.Media.Imaging
Module BlockIcon
''' <summary>
''' 块缩略图在picturebox中显示
''' </summary>
''' <param name="blockPath"></param>
''' <param name="blockName"></param>
''' <param name="pb">picturebox</param>
Public Sub GenerateBlockPreview(blockPath As String, blockName As String, pb As PictureBox)
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim ed As Editor = zwsoft.zwcad.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
Dim p As Point3d
'导入外部文件中的块
'这是个自定义函数,是将外部文件中的块复制到当前文档。
InsertExternalBlock(blockPath, blockName, 0, 0, p, 1, 1, 1, "0", 0, False, False)
Using trans As Transaction = db.TransactionManager.StartTransaction()
'打开块表
Dim bt As BlockTable = CType(trans.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
'循环遍历块表中的块表记录
For Each blockRecordId As ObjectId In bt
'打开块表记录对象
Dim btr As BlockTableRecord = CType(trans.GetObject(blockRecordId, OpenMode.ForRead), BlockTableRecord)
'如果是匿名块、布局块及没有预览图形的块,则返回
If btr.IsAnonymous OrElse btr.IsLayout OrElse Not btr.HasPreviewIcon Then Continue For
If btr.Name = blockName Then
'获取块的预览图,仅支持2014及以上CAD。
Dim imgsrc As Object = CMLContentSearchPreviews.GetBlockTRThumbnail(btr)
Dim bmp As Bitmap = ImageSourceToGDI(CType(imgsrc, System.Windows.Media.Imaging.BitmapSource))
' 将图像显示在 PictureBox 中
pb.Image = bmp
End If
Next
trans.Commit()
End Using
End Sub
Public Function ImageSourceToGDI(src As BitmapSource) As System.Drawing.Image
Dim ms As New MemoryStream()
Dim encoder As New BmpBitmapEncoder()
encoder.Frames.Add(BitmapFrame.Create(src))
encoder.Save(ms)
ms.Flush()
Return System.Drawing.Image.FromStream(ms)
End Function
End Module
CMLContentSearchPreviews.GetBlockTRThumbnail 这个接口在zwcad2022-2024都有问题,zwcad2025才修复了