Here is what i came up with in case anyone else search for this problem. The InsertBlock and SetAttributes code is from a book i bought called "VB.Net Programing for AutoCAD Customization" and i think it is a good book to learn with.
Public Shared Sub ImportBlock(ByVal BlockFile As String, ByVal BlockName As String)
Dim myDwg As ApplicationServices.Document
Dim myDwgLoc As DocumentLock
Dim mySourceDB As New Database
Dim myActiveDB As Database
'Read file and load block into current database
Try
myDwg = ApplicationServices.Application.DocumentManager.MdiActiveDocument
myDwgLoc = myDwg.LockDocument()
myActiveDB = myDwg.Database
mySourceDB.ReadDwgFile(BlockFile, IO.FileShare.Read, True, "")
myActiveDB.Insert(BlockName, mySourceDB, True)
mySourceDB.Dispose()
myDwgLoc.Dispose()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "AutoGen Error")
End Try
End Sub
Public Shared Function InsertBlock(ByVal InsPt As Geometry.Point3d, ByVal BlockName As String, Optional ByVal XScale As Double = 1.0, Optional ByVal YScale As Double = 1.0, Optional ByVal ZScale As Double = 1.0) As DatabaseServices.ObjectId
Try
Dim myTransMan As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction
Dim myDwg As ApplicationServices.Document
Dim myBT As DatabaseServices.BlockTable
Dim myBTR As DatabaseServices.BlockTableRecord
Dim myAttColl As DatabaseServices.AttributeCollection
Dim myEnt As DatabaseServices.Entity
Dim myBTREnum As BlockTableRecordEnumerator
Dim myDwgLoc As DocumentLock
'Get active document and begin transaction
myDwg = ApplicationServices.Application.DocumentManager.MdiActiveDocument
myDwgLoc = myDwg.LockDocument()
myTransMan = myDwg.TransactionManager
myTrans = myTransMan.StartTransaction
'Open Block Table
myBT = myDwg.Database.BlockTableId.GetObject(OpenMode.ForRead)
myBTR = myBT(DatabaseServices.BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)
'Insert the Block
Dim myBlockDef As BlockTableRecord = myBT(BlockName).GetObject(OpenMode.ForRead)
Dim myBlockRef As New DatabaseServices.BlockReference(InsPt, myBT(BlockName))
myBlockRef.ScaleFactors = New Geometry.Scale3d(XScale, YScale, ZScale)
myBTR.AppendEntity(myBlockRef)
myTrans.AddNewlyCreatedDBObject(myBlockRef, True)
'Set Attribute values
myAttColl = myBlockRef.AttributeCollection
myBTREnum = myBlockDef.GetEnumerator
While myBTREnum.MoveNext
myEnt = myBTREnum.Current.GetObject(OpenMode.ForWrite)
If TypeOf myEnt Is DatabaseServices.AttributeDefinition Then
Dim myAttDef As DatabaseServices.AttributeDefinition = myEnt
Dim myAttRef As New DatabaseServices.AttributeReference
myAttRef.SetAttributeFromBlock(myAttDef, myBlockRef.BlockTransform)
myAttColl.AppendAttribute(myAttRef)
myTrans.AddNewlyCreatedDBObject(myAttRef, True)
End If
End While
myDwgLoc.Dispose()
myTrans.Commit()
'Clean up
myTrans.Dispose()
myTransMan.Dispose()
Return myBlockRef.ObjectId
Catch e As Exception
MsgBox(e.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "AutoGen Error")
End Try
End Function
Public Shared Sub SetAttributes(ByVal BlockId As DatabaseServices.ObjectId, ByVal TagList As Collection)
Try
Dim myTransMan As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction
Dim myDwg As ApplicationServices.Document
Dim myDwgLoc As DocumentLock
Dim myBlkRef As DatabaseServices.BlockReference
Dim myAttColl As DatabaseServices.AttributeCollection
Dim myEnt As DatabaseServices.ObjectId
Dim myAttRef As DatabaseServices.AttributeReference
'Get active document and begin transaction
myDwg = ApplicationServices.Application.DocumentManager.MdiActiveDocument
myDwgLoc = myDwg.LockDocument()
myTransMan = myDwg.TransactionManager
myTrans = myTransMan.StartTransaction
'Find block and set attributes
myBlkRef = BlockId.GetObject(OpenMode.ForWrite)
myAttColl = myBlkRef.AttributeCollection
For Each myEnt In myAttColl
myAttRef = myEnt.GetObject(OpenMode.ForWrite)
Dim myItem As String
Try
myItem = TagList(myAttRef.Tag)
myAttRef.TextString = myItem
Catch
End Try
Next
myDwgLoc.Dispose()
myTrans.Commit()
'Clean up
myTrans.Dispose()
myTransMan.Dispose()
Catch e As Exception
MsgBox(e.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "AutoGen Error")
End Try
End Sub
Here is how you call them:
Dim myBlk As DatabaseServices.ObjectId
myBlk = DwgTools.InsertBlock(New Geometry.Point3d(0, 0, 0), "titlebka")
Dim myTags As New Collection
myTags.Add("me", "Drawn_By")
myTags.Add("whatever", "Drawing_Number")
DwgTools.SetAttributes(myBlk, myTags)
DwgTools.ImportBlock("f:\autocad_dwgs\symbols\agen\supply1.dwg", "ChrisW")
DwgTools.InsertBlock(New Geometry.Point3d(5, 5, 0), "chrisw")