.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Adding attribute to block from file

4 REPLIES 4
Reply
Message 1 of 5
myspambox111
327 Views, 4 Replies

Adding attribute to block from file

I see a ton of stuff on the forums and net about this and that is my problem. I don't know which really applies to what I'm doing. I just want the simple answer without having to delve into the more advanced problems of other posters.

That being said, I have a block I have inserted into my database from file. This block has attributes which will need to be changed from drawing to drawing based on external data. I would like to know how to access each of these attributes by name (tag?) and set their values. It seems this is done using attributeReferences and attributeDefinitions. Beyond that, I'm confused as the details of code I've found have to do with dynamic blocks, xrecords and like things not pertinent to my situation.

Help appreciated!
4 REPLIES 4
Message 2 of 5
myspambox111
in reply to: myspambox111

I found a partial solution on http://discussion.autodesk.com/forums/click.jspa?searchID=8059875&messageID=6024895

This allows me to loop through all the attributes using an enumerator but I still don't really understand the nature of attributeDefinition and Reference. I'd like a little more control by like using the actual name of the attribute and assigning the value. I'm sure I'll figure it out soon (hopefully) now that I have this working.



Edited by: myspambox111 on Jan 10, 2009 1:14 AM
Message 3 of 5

There's really not much to it.



Public Overridable Sub SetAttribute(ByVal bref As BlockReference, ByVal Tag As String, ByVal TextString As String)

If bref Is Nothing Then Exit Sub

If TextString Is Nothing Then Exit Sub

Using trans As Transaction = db.TransactionManager.StartTransaction

trans.GetObject(bref.ObjectId, OpenMode.ForWrite, True, True)

Dim attColl As AttributeCollection = bref.AttributeCollection

Dim att As AttributeReference

For I As Integer = 1 To attColl.Count

att = attColl(I - 1).GetObject(OpenMode.ForWrite, True, True)

If att.Tag = Tag Then

att.TextString = TextString

Exit For

End If

Next

trans.Commit()

End Using

End Sub



Hope this helps,



jvj
Message 4 of 5

I have not looked at Jamie's code, and I assume it is good, but I thought I'd add a comment to address your question about the nature of attribute definitions and attribute references.



the AutoCAD database contains Symbol Tables which define certain objects.... Without broadening this conversation too much, we're just talking about the Block Table, in this case. The block table contains the definition of a block, that is, what objects autocad is to replicate each time the block is inserted into the model, or paper space. This is where the concept of an AttributeDefinition comes in. When you are defining a block to autocad, which in .NET is creating a BlockTableRecord, you would add objects of type AttributeDefinition. In the AttributeDefinition, you can set properties, such as a default value, and a prompt to display in the user dialog for attribute editing, Height, Width, Constant, Invisible.... etc.



But, once a block definition has been inserted into a drawing, in Model or Paper space, it becomes a BlockReference, and if you iterate through all of the Sub-Entities of the BlockReference then the (attribute) objects you will find there will be type AttributeReference.



perhaps if you take away all the autocad stuff and just think in terms of programming, you could look at a block definition in a similar way to a class definition, and maybe compare the attribute to a property defined in the class. when you are writing the code at design time, you are creating the definition of what that class is and what each instance will contain, but at runtime you may create multiple instances of the class, which will in turn create multiple instances of the properties. These class instances created at runtime are like the BlockReference and the property instances that go with them are like AttributeReference.



Hope that helps a little...
Dave O.                                                                  Sig-Logos32.png
Message 5 of 5

Good explaination braincloud. And to add to that, the final step is to add the attribute to the attribute collection of the block reference. Think of this as adding an entity to the outside of the block not the inside. (like groupings)



That looks something like this:




attColl = br.AttributeCollection



For Each oid As ObjectId In btr 'accepts only key string



ent = trans.GetObject(oid, OpenMode.ForRead, True, True)



If TypeOf ent Is AttributeDefinition Then



Dim attdef As AttributeDefinition = ent



Dim attref As New AttributeReference



attref.SetAttributeFromBlock(attdef, br.BlockTransform)



attref.TextString = attdef.TextString



attColl.AppendAttribute(attref)



trans.AddNewlyCreatedDBObject(attref, True)



End If



Next oid



So for your new attribute definition, you will need to add it to the block reference's attribute collection of each block reference in your drawing.



good luck,



jvj

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost