Author Topic: VB.NET Insert Block eKeyNotFound  (Read 205 times)

0 Members and 1 Guest are viewing this topic.

sigster

  • Mosquito
  • Posts: 17
VB.NET Insert Block eKeyNotFound
« on: September 13, 2021, 05:49:08 AM »

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: [Select]


            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


Atook

  • Swamp Rat
  • Posts: 975
Re: VB.NET Insert Block eKeyNotFound
« Reply #1 on: September 13, 2021, 11:00:25 AM »
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

  • Mosquito
  • Posts: 17
Re: VB.NET Insert Block eKeyNotFound
« Reply #2 on: September 14, 2021, 04:20:36 AM »
The textbox was visible and did clear the name and I did not see it
so it was empty string