' (C) CodeHimBelonga kdub 2015/07/11
'
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports AcadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application
<Assembly: CommandClass(GetType(kdubRenameBlock.MyCommands))>
Namespace kdubRenameBlock
Public Class MyCommands
<CommandMethod("Test01", CommandFlags.Modal)> _
Public Sub Test01()
' get current document, database and editor.
Dim doc = AcadApp.DocumentManager.MdiActiveDocument
Dim db = doc.Database
Dim ed = doc.Editor
' establish variable
Const existingBlockName As String = "blockold"
Const renamedBlockName As String = "blocknew"
'start a document transaction.
Using tr As Transaction = db.TransactionManager.StartTransaction()
' open the blocktable for reading.
Dim bt = TryCast(tr.GetObject(db.BlockTableId, OpenMode.ForWrite), BlockTable)
' affirm that existingBlockName exists.
If Not bt.Has(existingBlockName) Then
ed.WriteMessage(vbLf & "Block '{0}' is not available in the current drawing.", existingBlockName)
Return
End If
' affirm that renamedBlockName does NOT exist.
If bt.Has(renamedBlockName) Then
ed.WriteMessage(vbLf & "Block '{0}' already exists in the current drawing.", renamedBlockName)
Return
End If
' open the block tableRecord for the existingBlockName
Dim blockTableRecordId = bt(existingBlockName)
Dim btr = TryCast(tr.GetObject(blockTableRecordId, OpenMode.ForWrite), BlockTableRecord)
ed.WriteMessage(vbLf & "DXF name: " & Convert.ToString(btr.ObjectId.ObjectClass.DxfName))
ed.WriteMessage(vbLf & "ObjectID: " & btr.ObjectId.ToString())
ed.WriteMessage(vbLf & "BlockName: " & Convert.ToString(btr.Name))
' rename the block
btr.Name = renamedBlockName
tr.Commit()
ed.WriteMessage(vbLf & "ObjectID: " & btr.ObjectId.ToString())
ed.WriteMessage(vbLf & "BlockName: " & Convert.ToString(btr.Name))
End Using
End Sub
End Class
End Namespace