Ad

Wednesday, November 23, 2011

Insert Drawing as Block

The way I’ve found to insert a drawing into another drawing as a block takes a surprisingly lot of code. This function I’ve created takes a file name, inserts a block and returns the ObjectId of the newly inserted block.

    Private Function InsertDwgAsBlock(ByVal fileName As String) As ObjectId
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database

Using tr As Transaction = db.TransactionManager.StartTransaction
Dim blkTable As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)

Dim blkTblRec As BlockTableRecord = blkTable(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)
Dim blkObjId As ObjectId
Using dbInsert As New Database(False, True)
dbInsert.ReadDwgFile(fileName, IO.FileShare.Read, True, "")
blkObjId = db.Insert(Path.GetFileNameWithoutExtension(fileName), dbInsert, True)
End Using

Dim blkRef As New BlockReference(New Point3d(0, 0, 0), blkObjId)

blkTblRec.AppendEntity(blkRef)
tr.AddNewlyCreatedDBObject(blkRef, True)
tr.Commit()
Return blkRef.ObjectId
End Using
End Function

The code doesn’t show how to get the filename, but there are plenty of examples on the internet to accomplish the task.

3 comments:

felipe M said...

How is this different from using the insert command?

Felipe M said...

How is this different from using the insert command provided by Autodesk?
Why?

Christopher Fugitt said...

It's if you want to do it in a vb.NET program. That way you can combine a bunch of steps.

LinkWithin

Blog Widget by LinkWithin

Ad