.NET
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
writing attribute values
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Hello,
I'm trying to write some code that adds values to attributes of a specified block.
I'm able to search for the block, but I'm having trouble aditing it...
Dim acDoc AsDocument = Application.DocumentManager.MdiActiveDocument
Dim acCurDb AsDatabase = acDoc.Database
Dim acTransMgr As Autodesk.AutoCAD.DatabaseServices.TransactionManager= acCurDb.TransactionManager
Using acTrans1 AsTransaction= acTransMgr.StartTransaction
Dim acBlkTbl AsBlockTable = acTrans1.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
If acBlkTbl.Has("blockname") Then
*** here I need to get the object found in the active block table and edit it's attributes by atribute name ****
End If
End Using
I hope aomebody was some simple-sample code I can use!!
Thanx,
Marc
Re: writing attribute values
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Below code you can edit att values by block
you can use it like
SetAttribute(ObjectId of block, "BLOCKNAME", "ATTNAME", "ATTVALUE")
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: writing attribute values
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Thanks for this!
The only thing; I don't know what to use in the ObjectId of block in the line:
SetAttribute(ObjectId of block, "BLOCKNAME", "ATTNAME", "ATTVALUE")
Marc
Re: writing attribute values
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
I send you whole class
Maybe it is helpful for you. object id is block object.objectid in short property of object
Take a look at the code; the main sub create a selection set on screen and filter all the blocks
in selection set then changed desired blocks att with SetAttribute Sub.
Let me know if it is helpful...
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime
Public Class Class1
<CommandMethod("SBEA")> _
Public Sub SBlocksEditAtts()
Dim acDocEd As Editor = Application.DocumentManager.MdiActiveDocument.Edit or
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
Dim acTypValAr(0) As TypedValue
acTypValAr.SetValue(New TypedValue(DxfCode.Start, "INSERT"), 0)
Dim acSelFtr As SelectionFilter = New SelectionFilter(acTypValAr)
Dim prselres As PromptSelectionResult = acDocEd.GetSelection(acSelFtr)
If prselres.Status = PromptStatus.OK Then
Dim acSSet As SelectionSet = prselres.Value
Dim nn As Integer = 0
For Each acSSObj As SelectedObject In acSSet
SetAttribute(acSSObj.ObjectId, "BLOCKNAME", "ATTNAME", nn.ToString)
nn = nn + 1
Next
acDocEd.WriteMessage("Number of objects selected: " & acSSet.Count.ToString())
End If
End If
acTrans.Commit()
End Using
End Sub
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
End Class
Re: writing attribute values
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
What the OP needs to realize is that the code as posted would be looking at BlockTableRecords, and what he really needs is BlockReferences.
That said, there have been countless posts on that subject over the last few years, so rather than re-hashing the same old stuff, I would suggest you do a search on this group, and you could also get some info by doing a search on the Through the Interface blog.
Try this one:
http://through-the-interface.typepad.com/through_t

