Message 1 of 4
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Using ACAD 2014, VB.NET code.
I am attempting to develop an app that has to insert block references into the active Paper Space layout. I've found numerous code examples for inserting blocks, including the recent one in this forum. Through various iterations of my code I still have not been successful. The code works for inserting blocks in Model space. The odd part about the Model space functionality is that I have to save and exit the drawing, then reopen it for the block to appear.
My function is included below. You will see the variations I have tried preceeded by the comments #Attempt 1, #Attempt 2, and #Attempt 3.
Thanks for any help you can provide.
Public Function InsertBlock(ByVal BlockName As String, ByVal ptPosition As Point3d, Optional ByVal inPaperSpace As Boolean = False, Optional ByVal LayoutName As String = "") As Boolean
' Get the current database and start a transaction
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Autodesk.AutoCAD.DatabaseServices.Database
acCurDb = Application.DocumentManager.MdiActiveDocument.Database
InsertBlock = False
Using acLckDocCur As DocumentLock = acDoc.LockDocument
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
' Open the Block table for read
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
Dim blkRecId As ObjectId = ObjectId.Null
If acBlkTbl.Has(BlockName) Then
blkRecId = acBlkTbl(BlockName)
Else
Exit Function
End If
' Insert the block reference into the current space
If blkRecId <> ObjectId.Null Then
Using acBlkRef As New BlockReference(New Point3d(0, 0, 0), blkRecId)
With acBlkRef
.SetDatabaseDefaults()
.Visible = True
.Position = ptPosition
.ScaleFactors = New Autodesk.AutoCAD.Geometry.Scale3d(10, 10, 10)
.Rotation = 0
End With
Dim acBlkTblRec As BlockTableRecord
'Attempt #1
'acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite)
'Attempt #2
If inPaperSpace Then
'acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.PaperSpace), OpenMode.ForWrite)
'Attempt #3
If LayoutName.Length = 0 Then
Exit Function
End If
Dim loMgr As LayoutManager = LayoutManager.Current
Dim loID As ObjectId = loMgr.GetLayoutId(LayoutName)
Dim lo As Layout = acTrans.GetObject(loID, OpenMode.ForRead)
acBlkTblRec = acTrans.GetObject(lo.BlockTableRecordId, OpenMode.ForWrite)
Else
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
End If
acBlkTblRec.AppendEntity(acBlkRef)
acTrans.AddNewlyCreatedDBObject(acBlkRef, True)
End Using
End If
' Save the new object to the database
acTrans.Commit()
' Dispose of the transaction
InsertBlock = True
End Using
End Using
End Function
Solved! Go to Solution.