I am inserting a block (strSectionBlk) into an AutoCAD 13 drawing using VB .NET. This block has an attribute with tag "FAB_HEIGHT" and I want to edit its string to the value of a variable "strFabHeight" at the time of the insertion of the block. The insertion of the block is fine but I've failed to edit the attibute. Could someone help me, please?
Public Sub InsertSection()
'Call the Function that decides the name of the head section block strSectionBlk
getSectionDwg(strModel, strHeadType)
'Start the database
Dim db As Database
db = HostApplicationServices.WorkingDatabase()
'Start the Editor
Dim ed As Editor
ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
'Start the transaction
Dim trans As Transaction
trans = db.TransactionManager.StartTransaction
Try
'Open the Block Table for Read
Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
'Open the Block Table Record for Write
Dim btr As BlockTableRecord
btr = trans.GetObject(bt.Item(BlockTableRecord.ModelSpace), Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)
'Read the file and insert the block
Dim id As ObjectId
Dim dbDwg As New Database(False, True)
dbDwg.ReadDwgFile(file path & strSectionBlk, IO.FileShare.Read, True, "")
id = db.Insert(strSectionBlk, dbDwg, True)
dbDwg.Dispose()
Dim ptInsert As New Point3d(0.875, 0.875, 0)
Dim blkRef As New BlockReference(ptInsert, id)
btr.AppendEntity(blkRef)
trans.AddNewlyCreatedDBObject(blkRef, True)
Editing of the attribute?
'Commit the transaction
trans.Commit()
Catch
MsgBox("Fail to Read Section Dwg File and Insert it in Current Database.")
Finally
trans.Dispose()
End Try
End Sub
Solved! Go to Solution.
Solved by truss_85. Go to Solution.
Solved by truss_85. Go to Solution.
I've tried a lot of coding. The latest is below. Does not work. What Am I missing?
Public Sub InsertSection()
'Call the Function that returns the section block name strSectionBlk
getSectionDwg(strModel, strHeadType)
'Start the database
Dim db As Database
db = HostApplicationServices.WorkingDatabase()
'Start the Editor
Dim ed As Editor
ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
'Start the transaction
Dim trans As Transaction
trans = db.TransactionManager.StartTransaction
Try
'Open the Block Table for Read
Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
'Open the Block Table Record for Write
Dim btr As BlockTableRecord
btr = trans.GetObject(bt.Item(BlockTableRecord.ModelSpace), Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)
'Read the file and insert the block
Dim id As ObjectId
Dim dbDwg As New Database(False, True)
dbDwg.ReadDwgFile("path\" & strSectionBlk, IO.FileShare.Read, True, "")
id = db.Insert(strSectionBlk, dbDwg, True)
dbDwg.Dispose()
Dim ptInsert As New Point3d(0.875, 0.875, 0)
Dim blkRef As New BlockReference(ptInsert, id)
btr.AppendEntity(blkRef)
trans.AddNewlyCreatedDBObject(blkRef, True)
'Get the Attribute Collection of the Block that is currently in use by Block Reference
Dim BlocRefAttrCollection As AttributeCollection = blkRef.AttributeCollection
For Each BlocRefAttrID As ObjectId In BlocRefAttrCollection
' Go through all AttributesReferences in the AttributesCollection
Dim BlocRefAttr As New AttributeReference
BlocRefAttr = trans.GetObject(BlocRefAttrID, OpenMode.ForWrite)
BlocRefAttr.Tag = "FAB_HEIGHT"
BlocRefAttr.TextString = "19"
Next
'Commit the transaction
trans.Commit()
Catch
MsgBox("Fail to Read Section Dwg File and Insert it in Current Database.")
Finally
trans.Dispose()
End Try
End Sub
I sent a sub below. Take a look at it. It might be handy. you can use sub to in your code.
Public Sub SetAttribute(ByVal BlockID As Autodesk.AutoCAD.DatabaseServices.ObjectId, ByVal blckname As String, ByVal AttTag As String, ByVal AttVal As String) Dim MyDb As Database = Application.DocumentManager.MdiActiveDocument.Database If BlockID.IsNull Then Exit Sub Try Using myTrans As Transaction = MyDb.TransactionManager.StartTransaction Dim myBlckRef As BlockReference Dim myAttColl As AttributeCollection Dim myBlckTable As BlockTableRecord myBlckRef = BlockID.GetObject(OpenMode.ForWrite) If myBlckRef.IsDynamicBlock Then myBlckTable = myTrans.GetObject(myBlckRef.DynamicBlockTableRecord, OpenMode.ForRead) Else myBlckTable = myTrans.GetObject(myBlckRef.BlockTableRecord, OpenMode.ForRead) End If If String.Compare(myBlckTable.Name, blckname, True) = 0 Then myAttColl = myBlckRef.AttributeCollection Dim myEnt As Autodesk.AutoCAD.DatabaseServices.ObjectId Dim myAttRef As Autodesk.AutoCAD.DatabaseServices.AttributeReference For Each myEnt In myAttColl myAttRef = myEnt.GetObject(OpenMode.ForWrite) If String.Compare(myAttRef.Tag, AttTag, True) = 0 Then myAttRef.TextString = AttVal.ToString End If Next End If myTrans.Commit() End Using Catch ex As Exception End Try End Sub