Doooh!
Yes. That is an old snippet from an old book written by an old man. For everyone's benefit, here is the new code from the new book written by a new and improved (at least I'm trying to improve) man.
I have supplied three SetAttributes Procedures. First, the ArrayList, ArrayList one (from the old book written by the old man). Second, using the HashTable, and Third, the Dictionary(Of String, String). The benefit of using the HashTable and Dictionary Classes is that they support the use of a Key by which we can get the Attribute Values instead of two separate ArrayLists. They are using Strings as their 'values' in these examples. Other Types can be used as well.
Thanks.
Jerry
{code}
_
Public Sub InsertBlocks()
Dim myBlkRefID As ObjectId = InsertBlock(New Point3d(0, 3, 0), "BlockA", 1, 1, 1)
Dim tagArray As New ArrayList
Dim valArray As New ArrayList
tagArray.Add("TAGA") : valArray.Add("not")
tagArray.Add("TAGB") : valArray.Add("the")
tagArray.Add("TAGC") : valArray.Add("best")
tagArray.Add("TAGD") : valArray.Add("way")
SetAttributes(myBlkRefID, tagArray, valArray)
myBlkRefID = InsertBlock(New Point3d(2, 3, 0), "BlockA", 1, 1, 1)
Dim myAttHash As New Hashtable
myAttHash.Add("TAGA", "A")
myAttHash.Add("TAGB", "B")
myAttHash.Add("TAGC", "C")
myAttHash.Add("TAGD", "D")
SetAttributes(myBlkRefID, myAttHash)
myBlkRefID = InsertBlock(New Point3d(4, 3, 0), "BlockA", 1, 1, 1)
Dim myAttDictionary As New Dictionary(Of String, String)
myAttDictionary.Add("TAGA", "1")
myAttDictionary.Add("TAGB", "2")
myAttDictionary.Add("TAGC", "3")
myAttDictionary.Add("TAGD", "4")
SetAttributes(myBlkRefID, myAttDictionary)
End Sub
Public Function InsertBlock(ByVal InsPt As Geometry.Point3d, _
ByVal BlockName As String, ByVal XScale As Double, _
ByVal YScale As Double, ByVal ZScale As Double) As DatabaseServices.ObjectId
Dim retObjectID As ObjectId = ObjectId.Null
Dim myDB As Database = HostApplicationServices.WorkingDatabase
Using myTrans As Transaction = myDB.TransactionManager.StartTransaction
'Open the database for Write
Dim myBT As BlockTable = myDB.BlockTableId.GetObject(OpenMode.ForRead)
Dim myBTR As BlockTableRecord = myBT(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)
'Check to see if the Block exists
If Not myBT.Has(BlockName) Then Return retObjectID
'Insert the Block
Dim myBlockDef As BlockTableRecord = myBT(BlockName).GetObject(OpenMode.ForRead)
Dim myBlockRef As New DatabaseServices.BlockReference(InsPt, myBT(BlockName))
myBlockRef.ScaleFactors = New Geometry.Scale3d(XScale, YScale, ZScale)
myBTR.AppendEntity(myBlockRef)
myTrans.AddNewlyCreatedDBObject(myBlockRef, True)
'Set the Attribute Value
Dim myAttColl As DatabaseServices.AttributeCollection
Dim myEnt As DatabaseServices.Entity
myAttColl = myBlockRef.AttributeCollection
For Each myOID As ObjectId In myBlockDef
myEnt = myOID.GetObject(OpenMode.ForWrite)
If TypeOf myEnt Is DatabaseServices.AttributeDefinition Then
Dim myAttDef As DatabaseServices.AttributeDefinition = myEnt
Dim myAttRef As New DatabaseServices.AttributeReference
myAttRef.SetAttributeFromBlock(myAttDef, myBlockRef.BlockTransform)
myAttColl.AppendAttribute(myAttRef)
myTrans.AddNewlyCreatedDBObject(myAttRef, True)
End If
Next
retObjectID = myBlockRef.ObjectId
'Commit the Transaction
myTrans.Commit()
End Using
Return retObjectID
End Function
Public Sub SetAttributes(ByVal BlockID As DatabaseServices.ObjectId, _
ByVal TagList As ArrayList, ByVal StringList As ArrayList)
Using myTrans As DatabaseServices.Transaction = BlockID.Database.TransactionManager.StartTransaction
Dim myBlkRef As DatabaseServices.BlockReference
Dim myAttColl As DatabaseServices.AttributeCollection
myBlkRef = BlockID.GetObject(OpenMode.ForWrite)
myAttColl = myBlkRef.AttributeCollection
Dim myAttID As DatabaseServices.ObjectId
Dim myAttRef As DatabaseServices.AttributeReference
For Each myAttID In myAttColl
myAttRef = myAttID.GetObject(OpenMode.ForWrite)
Dim X As Integer = TagList.IndexOf(myAttRef.Tag)
If X >= 0 Then
myAttRef.TextString = StringList(X)
End If
Next
myTrans.Commit()
End Using
End Sub
Public Sub SetAttributes(ByVal BlockID As DatabaseServices.ObjectId, _
ByVal AttValues As Hashtable)
Using myTrans As DatabaseServices.Transaction = BlockID.Database.TransactionManager.StartTransaction
Dim myBlkRef As DatabaseServices.BlockReference
Dim myAttColl As DatabaseServices.AttributeCollection
myBlkRef = BlockID.GetObject(OpenMode.ForWrite)
myAttColl = myBlkRef.AttributeCollection
Dim myAttID As DatabaseServices.ObjectId
Dim myAttRef As DatabaseServices.AttributeReference
For Each myAttID In myAttColl
myAttRef = myAttID.GetObject(OpenMode.ForWrite)
If AttValues.ContainsKey(myAttRef.Tag) Then
myAttRef.TextString = AttValues(myAttRef.Tag)
End If
Next
myTrans.Commit()
End Using
End Sub
Public Sub SetAttributes(ByVal BlockID As DatabaseServices.ObjectId, _
ByVal AttValues As Dictionary(Of String, String))
Using myTrans As DatabaseServices.Transaction = BlockID.Database.TransactionManager.StartTransaction
Dim myBlkRef As DatabaseServices.BlockReference
Dim myAttColl As DatabaseServices.AttributeCollection
myBlkRef = BlockID.GetObject(OpenMode.ForWrite)
myAttColl = myBlkRef.AttributeCollection
Dim myAttID As DatabaseServices.ObjectId
Dim myAttRef As DatabaseServices.AttributeReference
For Each myAttID In myAttColl
myAttRef = myAttID.GetObject(OpenMode.ForWrite)
If AttValues.ContainsKey(myAttRef.Tag) Then
myAttRef.TextString = AttValues(myAttRef.Tag)
End If
Next
myTrans.Commit()
End Using
End Sub
{code}