Hello all,
This is my first post here. Just got started programming AutoCAD about two days ago. In need of some help already. I am getting an error that is telling me eAlreadyInDB, unclear what this means or what to do to fix this. Here is the code I am using currently. The error is occurring at this snippet of code.
newBlockDef.AppendEntity(acEnt)
<CommandMethod("CreateBlock")> _
Public Sub SelectEnts()
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
Dim dwg As Database = doc.Database
Dim res As PromptSelectionResult = ed.GetSelection
Dim SS As Autodesk.AutoCAD.EditorInput.SelectionSet = res.Value
Dim idArray As ObjectId() = SS.GetObjectIds()
Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = dwg.TransactionManager
Dim trans As Transaction = tm.StartTransaction
If res.Status = PromptStatus.OK Then
Try
' create the new block definition
Dim newBlockDef As BlockTableRecord = New BlockTableRecord
newBlockDef.Name = "CreateBlockTest"
Dim blockTable As BlockTable = trans.GetObject(dwg.BlockTableId, OpenMode.ForRead)
If (blockTable.Has("CreateBlockTest") = False) Then
blockTable.UpgradeOpen()
' Add the BlockTableRecord
blockTable.Add(newBlockDef)
trans.AddNewlyCreatedDBObject(newBlockDef, True)
Dim acSSet As SelectionSet = res.Value
For Each acSSobj As SelectedObject In acSSet
If Not IsDBNull(acSSobj) Then
Dim acEnt As Entity = trans.GetObject(acSSobj.ObjectId, OpenMode.ForRead)
If Not IsDBNull(acEnt) Then
newBlockDef.AppendEntity(acEnt)
End If
End If
Next
Dim blockRefPointOptions As PromptPointOptions = New PromptPointOptions("Pick insertion point of BlockRef : ")
Dim blockRefPointResult As PromptPointResult = ed.GetPoint(blockRefPointOptions)
' test the Status of the PromptPointResult.
If (blockRefPointResult.Status <> PromptStatus.OK) Then
'If we got here then the GetPoint failed
trans.Dispose()
Return
End If
'Declare a BlockReference variable
Dim blockRef As BlockReference = New BlockReference(blockRefPointResult.Value, newBlockDef.ObjectId)
Dim curSpace As BlockTableRecord = trans.GetObject(dwg.CurrentSpaceId, OpenMode.ForWrite)
curSpace.AppendEntity(blockRef)
' Tell the transaction about the new block reference
trans.AddNewlyCreatedDBObject(blockRef, True)
' Commit the transaction
trans.Commit()
End If
Catch ex As Exception
' If an error occurs the details of the problem will be printed
' on the AutoCAD command line.
ed.WriteMessage("a problem occured because " + ex.Message)
Finally
'Dispose the transaction by calling the Dispose method
trans.Dispose()
End Try
End If
End Sub
Thanks in advance,
Ted