Ad

Monday, October 10, 2011

vb.NET Insert Block and Attributes

To insert a block into AutoCAD using .NET you may also need to insert the attributes of the block after you insert the block into the drawing. Unlike in VBA, you have to insert the attributes also, they don’t come along for the ride. Below is the code to insert the block into a drawing in the current space.

    Public Function InsertBlock(ByVal blkName As String, ByVal insPt As Point3d, ByVal xBlkScale As Double, _
ByVal yBlkScale As Double, ByVal zBlkScale As Double, ByVal ang As Double) 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 blkObjId As ObjectId = GetBlkRefObjIdByName(blkName)

Dim blkRef As BlockReference = New BlockReference(insPt, blkObjId)
blkRef.SetDatabaseDefaults()
blkRef.ScaleFactors = New Scale3d(xBlkScale, yBlkScale, zBlkScale)
blkRef.Rotation = ang

Dim blkTblRec As BlockTableRecord
' Assumes the current space was already changed to.
blkTblRec = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)

blkTblRec.AppendEntity(blkRef)
tr.AddNewlyCreatedDBObject(blkRef, True)

' add the attribute definitions.
Dim blkTblR As BlockTableRecord = blkObjId.GetObject(OpenMode.ForRead)
For Each objId As ObjectId In blkTblR
Dim obj As DBObject = objId.GetObject(OpenMode.ForRead)
If TypeOf obj Is AttributeDefinition Then
Dim ad As AttributeDefinition = objId.GetObject(OpenMode.ForRead)
Dim ar As AttributeReference = New AttributeReference()
ar.SetAttributeFromBlock(ad, blkRef.BlockTransform)
ar.Position = ad.Position.TransformBy(blkRef.BlockTransform)
blkRef.AttributeCollection.AppendAttribute(ar)
tr.AddNewlyCreatedDBObject(ar, True)
End If
Next

tr.Commit()
Return blkRef.ObjectId
End Using

End Function

5 comments:

Marcio said...

My friend

Please, I have a doubt, the GetBlkRefObjIdByName is a special routine? I didn´t found it on the AutoCAD VB.NET reference.

Thanks

Marcio
marcio_buss@hotmail.com

Christopher Fugitt said...

Yes, it's the code used to select the block. You can do a Google search to find how to select an object using vb.NET.

Anonymous said...

Dear

I´m tried to find any routine, but without success. In really, I would like to insert a new dwg file into a actual drawing. Have you ane routine for this application? I test some on internet, but the "db.ReadDwgFile(fname, System.IO.FileShare.ReadWrite, True, vbNullString) 'Nothing)" return "eNoFiler", and I could not solve this problem.

Thanks

Marcio

iamMental said...

where is the information about the GetBlkRefObjIdByName this is not recognized in 2012 using vb.net 2010. if there is an import or refernece i need i cannot find it online. and when i google.. i just find copies of THIS post on other forums

Christopher Fugitt said...
This comment has been removed by the author.

LinkWithin

Blog Widget by LinkWithin