'原文:http://through-the-interface.typepad.com/through_the_interface/2011/11/gen ... -blocks-in-an-autocad-drawing-using-net.html
'VB.Net翻译转换 田草博客 www.tiancao.net
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime
Imports System.IO
Namespace BlockPreviews
Public Class Commands
<CommandMethod("GBP", CommandFlags.Session)> _
Public Shared Sub GenerateBlockPreviews()
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
Dim res As PromptFileNameResult = ed.GetFileNameForOpen("选择你要生成预览图像的文件:")
If res.Status <> PromptStatus.OK Then
Return
End If
Dim doc As Document = Nothing
Try
doc = Application.DocumentManager.Open(res.StringResult, False)
Catch
ed.WriteMessage(vbLf & "图形文件无效.")
Return
End Try
Dim db As Database = doc.Database
Dim path As String = System.IO.Path.GetDirectoryName(res.StringResult), name As String = System.IO.Path.GetFileName(res.StringResult), iconPath As String = path + "\" + name + " icons"
Dim numIcons As Integer = 0
Dim tr As Transaction = doc.TransactionManager.StartTransaction()
Using tr
Dim table As BlockTable = CType(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
For Each blkId As ObjectId In table
Dim blk As BlockTableRecord = CType(tr.GetObject(blkId, OpenMode.ForRead), BlockTableRecord)
' 忽略布局和匿名块
If blk.IsLayout OrElse blk.IsAnonymous Then
Continue For
End If
' 当缩略图不在时,生成缩略图
If blk.PreviewIcon Is Nothing Then
Dim ActiveDocument As Object = doc.AcadDocument
Dim data As Object() = {"_.BLOCKICON " + blk.Name + vbLf}
ActiveDocument.[GetType]().InvokeMember("SendCommand", System.Reflection.BindingFlags.InvokeMethod, Nothing, ActiveDocument, data)
End If
' 现在所有块都有了缩略图
If blk.PreviewIcon IsNot Nothing Then
' 创建一个输出文件夹
If Not Directory.Exists(iconPath) Then
Directory.CreateDirectory(iconPath)
End If
' 保存缩略图至文件夹
blk.PreviewIcon.Save(iconPath + "\" + blk.Name + ".bmp")
' 图标计数器加1
System.Math.Max(System.Threading.Interlocked.Increment(numIcons), numIcons - 1)
End If
Next
tr.Commit()
End Using
doc.CloseAndDiscard()
ed.WriteMessage(vbLf & "{0} block icons saved to ""{1}"".", numIcons, iconPath)
End Sub
End Class
End Namespace
'VB.Net翻译转换 田草博客 www.tiancao.net
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime
Imports System.IO
Namespace BlockPreviews
Public Class Commands
<CommandMethod("GBP", CommandFlags.Session)> _
Public Shared Sub GenerateBlockPreviews()
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
Dim res As PromptFileNameResult = ed.GetFileNameForOpen("选择你要生成预览图像的文件:")
If res.Status <> PromptStatus.OK Then
Return
End If
Dim doc As Document = Nothing
Try
doc = Application.DocumentManager.Open(res.StringResult, False)
Catch
ed.WriteMessage(vbLf & "图形文件无效.")
Return
End Try
Dim db As Database = doc.Database
Dim path As String = System.IO.Path.GetDirectoryName(res.StringResult), name As String = System.IO.Path.GetFileName(res.StringResult), iconPath As String = path + "\" + name + " icons"
Dim numIcons As Integer = 0
Dim tr As Transaction = doc.TransactionManager.StartTransaction()
Using tr
Dim table As BlockTable = CType(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
For Each blkId As ObjectId In table
Dim blk As BlockTableRecord = CType(tr.GetObject(blkId, OpenMode.ForRead), BlockTableRecord)
' 忽略布局和匿名块
If blk.IsLayout OrElse blk.IsAnonymous Then
Continue For
End If
' 当缩略图不在时,生成缩略图
If blk.PreviewIcon Is Nothing Then
Dim ActiveDocument As Object = doc.AcadDocument
Dim data As Object() = {"_.BLOCKICON " + blk.Name + vbLf}
ActiveDocument.[GetType]().InvokeMember("SendCommand", System.Reflection.BindingFlags.InvokeMethod, Nothing, ActiveDocument, data)
End If
' 现在所有块都有了缩略图
If blk.PreviewIcon IsNot Nothing Then
' 创建一个输出文件夹
If Not Directory.Exists(iconPath) Then
Directory.CreateDirectory(iconPath)
End If
' 保存缩略图至文件夹
blk.PreviewIcon.Save(iconPath + "\" + blk.Name + ".bmp")
' 图标计数器加1
System.Math.Max(System.Threading.Interlocked.Increment(numIcons), numIcons - 1)
End If
Next
tr.Commit()
End Using
doc.CloseAndDiscard()
ed.WriteMessage(vbLf & "{0} block icons saved to ""{1}"".", numIcons, iconPath)
End Sub
End Class
End Namespace
【BlockPreviews.rar】点击下载此文件
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |