.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

eKeyNotFound

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
Anonymous
3457 Views, 9 Replies

eKeyNotFound

I couldn't find why?can somebody help me ?I just want clone the block to the active CAD window from  the unopened DWGfile.

Public Sub InserBlock()
        Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim acDatabase As Database = acDoc.Database
        Dim acEditor As Editor = acDoc.Editor
        Dim pPtRes As PromptPointResult
        Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")
        pPtOpts.Message = vbLf & "insert the point:"
        pPtRes = acEditor.GetPoint(pPtOpts)
        Dim InsertPt As Point3d = pPtRes.Value
        If pPtRes.Status = PromptStatus.Cancel Then
            Exit Sub
        End If
                Using trans As Transaction = acDatabase.TransactionManager.StartTransaction
                Dim acBlockTable As BlockTable = trans.GetObject(acDatabase.BlockTableId, OpenMode.ForRead)
                Dim FilePath As String = "D:\2RD\PMDS\PMDS\Resources\PFD.dwg"
                Dim FileName As String = "PFD.dwg"
                Dim BlockName As String = "TextBox1.Text"

                If acBlockTable.Has(BlockName) = True Then
                    Dim Block As BlockReference = New BlockReference(InsertPt, acBlockTable(BlockName))
                    Block.ScaleFactors = New Scale3d(1, 1, 1)
                    Block.Rotation = 0

                    Dim ModelSpace As BlockTableRecord = trans.GetObject(acDatabase.CurrentSpaceId, OpenMode.ForWrite)
                    ModelSpace.AppendEntity(Block)

                    trans.AddNewlyCreatedDBObject(Block, True)
                    trans.Commit()
                Else
                    Dim db As New Database(False, False)
                    db.ReadDwgFile(FilePath, IO.FileShare.Read, False, "")
                    Dim id As ObjectId = acDatabase.Insert(FileName, db, False)
                    If id.IsValid = False Then
                        Exit Sub
                    End If
                    Dim Block As BlockReference = New BlockReference(InsertPt, acBlockTable(BlockName))
                    Block.ScaleFactors = New Scale3d(1, 1, 1)
                    Block.Rotation = 0
                    Dim ModelSpace As BlockTableRecord = trans.GetObject(acDatabase.CurrentSpaceId, OpenMode.ForWrite)
                    ModelSpace.AppendEntity(Block)
                    trans.AddNewlyCreatedDBObject(Block, True)
                    trans.Commit()
                End If
            End Using
    End Sub

9 REPLIES 9
Message 2 of 10
SENL1362
in reply to: Anonymous

Shouldn't you insert BlockName instead of FileName

 

 

Screenshot_2.png

Message 3 of 10
Anonymous
in reply to: SENL1362

yes! see this Dim FilePath As String = "D:\2RD\PMDS\PMDS\Resources\PFD.dwg" 

Message 4 of 10
Anonymous
in reply to: SENL1362

I‘ve alresdy change the Filename  to Blockname ,BUT why the programe dose not success!

Message 5 of 10
Anonymous
in reply to: SENL1362

I've use the blockname=9020101 that the PFD.DWG also exist.but it also don't success

Public Sub InserBlock()
Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim acDatabase As Database = acDoc.Database
Dim acEditor As Editor = acDoc.Editor
Dim pPtRes As PromptPointResult
Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")
pPtOpts.Message = vbLf & "Insert the point:"
pPtRes = acEditor.GetPoint(pPtOpts)
Dim InsertPt As Point3d = pPtRes.Value
If pPtRes.Status = PromptStatus.Cancel Then
Exit Sub
End If
Using (acDoc.LockDocument)
Using trans As Transaction = acDatabase.TransactionManager.StartTransaction
Dim acBlockTable As BlockTable = trans.GetObject(acDatabase.BlockTableId, OpenMode.ForRead)
Dim FilePath As String = "D:\2RD\PMDS\PMDS\Resources\PFD.dwg"
Dim FileName As String = "PFD.dwg"
Dim BlockName As String = "9020101"

If acBlockTable.Has(BlockName) = True Then
Dim Block As BlockReference = New BlockReference(InsertPt, acBlockTable(BlockName))
Block.ScaleFactors = New Scale3d(1, 1, 1)
Block.Rotation = 0

Dim ModelSpace As BlockTableRecord = trans.GetObject(acDatabase.CurrentSpaceId, OpenMode.ForWrite)
ModelSpace.AppendEntity(Block)

trans.AddNewlyCreatedDBObject(Block, True)
trans.Commit()
Else
Dim db As New Database(False, False)
db.ReadDwgFile(FilePath, IO.FileShare.Read, False, "")
Dim id As ObjectId = acDatabase.Insert(BlockName, db, False)
If id.IsValid = False Then
MsgBox("block""&BlockName&""not exist")
Exit Sub
End If
Dim Block As BlockReference = New BlockReference(InsertPt, acBlockTable(BlockName))
Block.ScaleFactors = New Scale3d(1, 1, 1)
Block.Rotation = 0
Dim ModelSpace As BlockTableRecord = trans.GetObject(acDatabase.CurrentSpaceId, OpenMode.ForWrite)
ModelSpace.AppendEntity(Block)
trans.AddNewlyCreatedDBObject(Block, True)
trans.Commit()
End If
End Using
End Using
End Sub

Message 6 of 10
Anonymous
in reply to: Anonymous

I've change l little ,it success ,butWhy all  of bolck in the PFD.DWG has copy to the  new CAD FILE???  where the problem?

Message 7 of 10
SENL1362
in reply to: Anonymous

Oh, now i understand you're problem.

The next line will insert the complete "pdf.dwg" into the acDoc Database as Block=BlockName:

Dim id As ObjectId = acDatabase.Insert(FileName, db, False)

 

Have a look at this sample to copy only individual Blocks into you're current Database:

http://through-the-interface.typepad.com/through_the_interface/2006/08/import_blocks_f.html

 

and change the part:

            // Only add named & non-layout blocks to the copy list

            if (!btr.IsAnonymous && !btr.IsLayout)

 

into

            // Only add named & non-layout blocks to the copy list

            if (!btr.IsAnonymous && !btr.IsLayout && btr.Name==BlockName)

 

(Although i'll prever : Regex.IsMatch(btr.name,BlockName,RegexOptions.IgnoreCase, to prevent Upper/Lower Case issues and allow the user to specify partial BlockName and other regular expressions).

 

 

 

 

Message 8 of 10
Anonymous
in reply to: SENL1362

so thanks with u,and can u help me more?

I've already see the blog from 

http://through-the-interface.typepad.com/through_the_interface/2006/08/import_blocks_f.html

I use the above code in my program,it success,but all the blocks in the unopen dwg file copy to the new dwg file.it's not what i want. 

and  now the new problem is 

Dim Block As BlockReference = New BlockReference(InsertPt, sourceDb(BlockName))    sourceDb in  the sourceDb(BlockName) have the problem that “Autodesk.AutoCAD.DatabaseServices.Database don't hanve the default attribute,and therefore can not be indexed" 

How can i solve the problem with VB.NET ?where the problem ?what should I do ? so thanks!

Here all the code :

Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim acEditor As Editor = acDoc.Editor
        Dim acDatabase As Database = acDoc.Database
        Dim pPtRes As PromptPointResult
        Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")
        Dim FilePath As String = "D:\2RD\PMDS\PMDS\Resources\PFD.dwg"
        Dim FileName As String = " PFD.dwg"
        Dim BlockName As String = "0920101"
        pPtOpts.Message = vbLf & "Insert Point of Block:"
        pPtRes = acEditor.GetPoint(pPtOpts)
        Dim InsertPt As Point3d = pPtRes.Value

        If pPtRes.Status = PromptStatus.Cancel Then
            Exit Sub
        End If
        Using (acDoc.LockDocument)
            Using sourceDb As New Database(False, False)
                sourceDb.ReadDwgFile(FilePath, System.IO.FileShare.Read, True, Nothing)
                sourceDb.CloseInput(True)
                Using trans As Transaction = acDatabase.TransactionManager.StartTransaction
                    Dim acBlockTable As BlockTable = DirectCast(trans.GetObject(acDatabase.BlockTableId, OpenMode.ForWrite), BlockTable)
                    If acBlockTable.Has(BlockName) = True Then
                        Dim Block As BlockReference = New BlockReference(InsertPt, sourceDb(BlockName))
                        Block.ScaleFactors = New Scale3d(1, 1, 1)
                        Block.Rotation = 0
                        Dim ModelSpace As BlockTableRecord = trans.GetObject(acDatabase.CurrentSpaceId, OpenMode.ForWrite)
                        ModelSpace.AppendEntity(Block)

                        trans.AddNewlyCreatedDBObject(Block, True)
                        trans.Commit()
                    Else
                        Dim db As New Database(False, False)
                        db.ReadDwgFile(FilePath, IO.FileShare.Read, False, "")
                        Dim id As ObjectId = sourceDb.Insert(FileName, db, False)

                        If id.IsValid = False Then
                            Exit Sub
                        End If

                        Dim Block As BlockReference = New BlockReference(InsertPt, acBlockTable(BlockName))
                        Block.ScaleFactors = New Scale3d(1, 1, 1)
                        Block.Rotation = 0

                        Dim ModelSpace As BlockTableRecord = trans.GetObject(acDatabase.CurrentSpaceId, OpenMode.ForWrite)
                        ModelSpace.AppendEntity(Block)

                        trans.AddNewlyCreatedDBObject(Block, True)
                        trans.Commit()

                    End If
                End Using
            End Using
        End Using
Message 9 of 10
kelidesign
in reply to: Anonymous

There is a big essential difference between the two copies. The Insert method will copy all the blocks into it. The name is arbitrary, but when your name has the same name as the block in the copied target database, it will be ignored. But it does not affect the copying of blocks in the file. It is worth mentioning that if there is a bug in the autocad block preview, when you cancel it, the preview will not be rolled back and repaired.

Therefore, you need to calmly think about what you want or don’t want in your expression. I don’t fully understand your appeal. Do you just want to know how to solve the eKeyNotFound exception problem based on the current code? If so, I have also encountered it and am trying to figure out a solution. If I find one later I'll be happy to share.

Message 10 of 10
kelidesign
in reply to: kelidesign

Because I made a fatal mistake, a design flaw in overloading? I'll update this post with some of my progress.

https://forums.autodesk.com/t5/autocad-forum/database-adddbobject-vs-transactionmanager/td-p/1271690...

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost