Visual Basic Customization
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
How to edit attributes in a block at insertion time with VB .NET?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.D
'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.ModelSpac
'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.
Re: How to edit attributes in a block at insertion time with VB .NET?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.D
'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.ModelSpac
'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
Re: How to edit attributes in a block at insertion time with VB .NET?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.Data base
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.DynamicBlockTableRecor d, 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.AttributeReferen ce
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
Re: How to edit attributes in a block at insertion time with VB .NET?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
You must open for write block table record not att. collection. That is the problem you need to fix.
Re: How to edit attributes in a block at insertion time with VB .NET?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Thanks truss_85!
That was the problem>
Re: How to edit attributes in a block at insertion time with VB .NET?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
I'm glad that I helped.
