<CommandMethod("test")>
Public Sub test()
Dim zDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim zDb As Database = zDoc.Database
Dim zEd As Editor = zDoc.Editor
Using mTrans As Transaction = zDb.TransactionManager.StartTransaction()
Try
Dim mBlkTbl As BlockTable = mTrans.GetObject(zDb.BlockTableId, OpenMode.ForWrite)
Dim zBlkTblRec As BlockTableRecord = mTrans.GetObject(mBlkTbl("test"), OpenMode.ForWrite)
Dim i As Integer
For Each zBlkTableRecId As ObjectId In zBlkTblRec.GetBlockReferenceIds(True, False)
Dim BlkRef As BlockReference = mTrans.GetObject(zBlkTableRecId, OpenMode.ForWrite)
Dim zMap As New IdMapping
Dim newzBlkTblRec As BlockTableRecord = zBlkTblRec.DeepClone(mBlkTbl, zMap, True)
newzBlkTblRec.Name = zBlkTblRec.Name & i
BlkRef.BlockTableRecord = mBlkTbl.Add(newzBlkTblRec)
mTrans.AddNewlyCreatedDBObject(newzBlkTblRec, True)
i = i + 1
Next
Catch ex As SystemException
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical, "Rename block.")
End Try
mTrans.Commit()
End Using
End Sub
<CommandMethod("test")>
Public Sub test()
Dim zDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim zDb As Database = zDoc.Database
Dim zEd As Editor = zDoc.Editor
Using mTrans As Transaction = zDb.TransactionManager.StartTransaction()
Try
Dim mBlkTbl As BlockTable = mTrans.GetObject(zDb.BlockTableId, OpenMode.ForWrite)
Dim zBlkTblRec As BlockTableRecord = mTrans.GetObject(mBlkTbl("test"), OpenMode.ForWrite)
Dim i As Integer
For Each zBlkTableRecId As ObjectId In zBlkTblRec.GetBlockReferenceIds(True, False)
Dim BlkRef As BlockReference = mTrans.GetObject(zBlkTableRecId, OpenMode.ForWrite)
Dim zMap As New IdMapping
Dim newzBlkTblRec As BlockTableRecord = zBlkTblRec.DeepClone(mBlkTbl, zMap, True)
newzBlkTblRec.Name = zBlkTblRec.Name & i
BlkRef.BlockTableRecord = mBlkTbl.Add(newzBlkTblRec)
mTrans.AddNewlyCreatedDBObject(newzBlkTblRec, True)
i = i + 1
Next
Catch ex As SystemException
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical, "Rename block.")
End Try
mTrans.Commit()
End Using
End Sub
I need to rename each block like this by example if the name of the block is "TEST" , all the blocks which are the same name will be renamed as "TEST0", "TEST1", "TEST2" etc. My code does it good.I think you need to name the different block References with an Attribute. It does not make sense to me to have many duplicate definitions of exactly the same thing just to give them a different name. It would be the same (in code) as copy-pasting exactly the same code to create a new static class every time you wanted a new instance of exactly the same thing with a different name.
When I try to save my drawing, I have the message below on the AutoCAD Text Window : "*Warning* Multiply owned object, handle "193AE""My guess is that you have one block reference trying to associate itself with multiple block definitions. Inside the loop that creates the new definitions you need to associate each individual reference to its individual definition.