Hello Everyone
So i have a function that creates a block for each selection of my choise of block
For each block the name si the same "Block1"
But i got an error on screen that said "e.DublicateRecordName"
How to avoid that?
Please help.Thankyou
Here is my function
Public Sub TestClone()
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
' Dim acCurDb As Database = acDoc.Database
Dim acCurDb As Autodesk.AutoCAD.DatabaseServices.Database
acCurDb = Application.DocumentManager.MdiActiveDocument.Database
acDoc.LockDocument()
Dim cEnt As Entity
Dim values() As TypedValue = { _
New TypedValue(DxfCode.BlockName, "DOT")
}
Dim sfilter As New SelectionFilter(values)
Dim SelOpts As New PromptSelectionOptions()
SelOpts.MessageForAdding = "Select Block"
SelOpts.AllowDuplicates = True
'Make the selection:
Dim res As PromptSelectionResult = ed.GetSelection(SelOpts, sfilter)
If Not res.Status = PromptStatus.OK Then Return
Dim SS As Autodesk.AutoCAD.EditorInput.SelectionSet = res.Value
Dim idarray As ObjectId() = SS.GetObjectIds()
Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
Dim myT As Transaction = tm.StartTransaction()
Dim dwg As Document = CadApp.DocumentManager.MdiActiveDocument
'Dim myT As Transaction = acCurDb.TransactionManager.StartTransaction()
Dim acBlkTbl As BlockTable
acBlkTbl = myT.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
'Dim acBlkTblRec As BlockTableRecord
Dim blkrecId As Object = ObjectId.Null
Using acBlkTblRec As New BlockTableRecord
Dim newBlockName As String = "Block1"
acBlkTblRec.Name = newBlockName
For Each id In idarray
cEnt = tm.GetObject(id, OpenMode.ForRead, False)
Dim bref As BlockReference = DirectCast(tm.GetObject(id, OpenMode.ForRead), BlockReference)
Dim x As Double
Dim y As Double
Dim z As Double
x = bref.Position.X
y = bref.Position.Y
z = bref.Position.Z
acBlkTblRec.Origin = (New Point3d(x, y, z))
'Dim acpoint As DBPoint = New DBPoint(New Point3d(x, y, z))
Using acpoint As DBPoint = New DBPoint(New Point3d(x, y, z))
' Dim layer As String
' layer = "MyAttributte"
'acpoint.Layer = layer
acBlkTblRec.AppendEntity(acpoint)
acBlkTbl.UpgradeOpen()
acBlkTbl.Add(acBlkTblRec)
'myT.AddNewlyCreatedDBObject(acpoint, True)
myT.AddNewlyCreatedDBObject(acBlkTblRec, True)
acCurDb.Pdmode = 32
acCurDb.Pdsize = 1
blkrecId = acBlkTblRec.Id
Dim acBlkRef As New BlockReference(New Point3d(x, y, z), blkrecId)
Dim acCurSpaceBlkTblRec As BlockTableRecord
acCurSpaceBlkTblRec = myT.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite)
acCurSpaceBlkTblRec.AppendEntity(acBlkRef)
myT.AddNewlyCreatedDBObject(acBlkRef, True)
End Using
Next
End Using
myT.Commit()
myT.Dispose()
End Sub
Solved! Go to Solution.
Solved by hgasty1001. Go to Solution.
Hi,
You can't add 2 block definitions with same name to the database, try asigning a counter and increment it each time a block is created and asign the name of the block using that counter ("Block"+counter.tostring), or use some other unique name generation mechanism (the current date up to miliseconds could work)
Gaston Nunez