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
Solved! Go to Solution.
Solved by SENL1362. Go to Solution.
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
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?
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).
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
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.