Visual Basic Customization

Reply
Active Member
kike55
Posts: 8
Registered: ‎01-29-2013
Message 1 of 6 (463 Views)
Accepted Solution

How to edit attributes in a block at insertion time with VB .NET?

463 Views, 5 Replies
01-31-2013 07:53 AM

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

Active Member
kike55
Posts: 8
Registered: ‎01-29-2013
Message 2 of 6 (440 Views)

Re: How to edit attributes in a block at insertion time with VB .NET?

02-01-2013 06:22 AM in reply to: kike55

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

Mentor
truss_85
Posts: 173
Registered: ‎02-13-2011
Message 3 of 6 (431 Views)

Re: How to edit attributes in a block at insertion time with VB .NET?

02-01-2013 08:06 AM in reply to: kike55

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

 

Mentor
truss_85
Posts: 173
Registered: ‎02-13-2011
Message 4 of 6 (429 Views)

Re: How to edit attributes in a block at insertion time with VB .NET?

02-01-2013 08:12 AM in reply to: kike55

You must open for write block table record not att. collection. That is the problem you need to fix.

Active Member
kike55
Posts: 8
Registered: ‎01-29-2013
Message 5 of 6 (410 Views)

Re: How to edit attributes in a block at insertion time with VB .NET?

02-06-2013 01:00 PM in reply to: truss_85

Thanks truss_85!

That was the problem>

Mentor
truss_85
Posts: 173
Registered: ‎02-13-2011
Message 6 of 6 (386 Views)

Re: How to edit attributes in a block at insertion time with VB .NET?

02-12-2013 10:59 PM in reply to: kike55

I'm glad that I helped.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community