Code Red > .NET

VB.NET Insert Block eKeyNotFound

(1/1)

sigster:

Hi

if I use blkName not "BPF" then I get error eKeyNotFound any hint how I fix this

 Dim btr As BlockTableRecord = TryCast(tr.GetObject(bt(blkName), OpenMode.ForRead), BlockTableRecord)
 Dim btr As BlockTableRecord = TryCast(tr.GetObject(bt("BPF"), OpenMode.ForRead), BlockTableRecord)

Thanks
Sigster


--- Code: ---

            Dim blk As String = Lab_block_name.Text
            Testblockinsert(blk)



   Public Sub Testblockinsert(ByVal blkName As String)

        Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
        Dim db As Database = HostApplicationServices.WorkingDatabase

        ed.WriteMessage(blkName & vbCr)

        Using tr As Transaction = db.TransactionManager.StartTransaction()
            Dim bt As BlockTable = TryCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
            Dim btr As BlockTableRecord = TryCast(tr.GetObject(bt("BPF"), OpenMode.ForRead), BlockTableRecord)

            If btr IsNot Nothing Then
                Dim ent As New BlockReference(New Point3d(0, 0, 0), btr.ObjectId)
                Dim modelspace As BlockTableRecord = TryCast(tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
                modelspace.AppendEntity(ent)
                tr.AddNewlyCreatedDBObject(ent, True)

                Dim dict As New Dictionary(Of AttributeReference, AttributeDefinition)()
                If btr.HasAttributeDefinitions Then
                    For Each id As ObjectId In btr
                        Dim obj As DBObject = tr.GetObject(id, OpenMode.ForRead)
                        If TypeOf obj Is AttributeDefinition Then
                            Dim ad As AttributeDefinition = TryCast(obj, AttributeDefinition)
                            Dim ar As New AttributeReference()
                            ar.SetAttributeFromBlock(ad, ent.BlockTransform)
                            ent.AttributeCollection.AppendAttribute(ar)
                            tr.AddNewlyCreatedDBObject(ar, True)
                            dict.Add(ar, ad)
                        End If
                    Next
                End If

                If BlockJigInsert.Jig(ent, dict) Then
                    tr.Commit()
                Else
                    ent.Dispose()
                    tr.Abort()
                End If
            End If
        End Using
        ' End If
    End Sub


--- End code ---

Atook:
What's the value of blkName when you get the error?

Seems it should work if blkName="BPF"

Any reason you're passing it in ByVal?

sigster:
The textbox was visible and did clear the name and I did not see it
so it was empty string   

Navigation

[0] Message Index

Go to full version