PDA

View Full Version : Block Reference & Attributes



bweir
2007-12-13, 04:42 PM
Why is it that when you create a new BlockReference object it will not have AttributeReferences created? What needs to be done to add the AttributeReferences that are defined in the BlockRecordTable?

ivan.markov
2008-01-02, 02:47 PM
Hi there,

I have encountered the same problem myself. The solution to that is to manually add the attributeReferences. You can do that by iterating through all the objects associated with BlockTableRecord. For each object check if the type is attributeDefinition. If it is, build a new attributeReference using that attDef and then attach the attributeReference to the atributeReference Collection of the BlockReference. If this explanation is unclear let me know and I will try to post some code that might help you.

runerdooner
2008-01-02, 11:54 PM
I was surprised by this to:

Autodesk give an example in Lab 5 of their tutorial for .net.

As mentioned above but instead of searching the whole block table you can get the objectID for the block and search through that for attribute reference.

Not sure what happens when you have several attributes in the block though.
I guess as you cycle through the block attributes you could query its tag name. Maybe does not matter as long as you have the correct number of attributes created in the block - you can reset all its values when you create blockref.

bweir
2008-01-18, 09:01 PM
Any ideas how this will affect attributes that are moved in dynamic blocks?

bweir
2008-01-25, 08:36 PM
If this explanation is unclear let me know and I will try to post some code that might help you.

Your explanation seems straight forward enough but I'm having problems with this still. I've tried to append attributes to the collection but funny things happen to the block and still I don't always get attributes. When I do get attributes I can't edit them, see the attachment for the error message when editing the attribute. Do you know how this will impact dynamic blocks? Many of our blocks use dynamics and appending the attributes really seems to muck it up unless I'm doing something wrong.

bweir
2008-01-25, 10:53 PM
Here is my code. The attributes will show up in the Property Palette and I can double click the block to edit the attributes but the attributes do not show in the drawing and sometimes when editing the attributes I get the error mentioned in the previous post.


Public Sub ApplyAttributeDefinisions(ByRef BlockReference As Autodesk.AutoCAD.DatabaseServices.BlockReference)
Dim Block As AcadDb.BlockTableRecord = BlockReference.BlockTableRecord.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
If Not Block.HasAttributeDefinitions Then Exit Sub
If Block.Database Is Nothing Then Exit Sub

Dim Trans As AcadDb.Transaction = Block.Database.TransactionManager.StartTransaction
Try
If BlockReference.Database Is Nothing Then Block.Database.AddDBObject(BlockReference)

For Each Id As AcadDb.ObjectId In Block
Dim Ent As AcadDb.Entity = Id.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)

If TypeOf Ent Is AcadDb.AttributeDefinition Then
Dim AttDef As AcadDb.AttributeDefinition = Ent
Dim AttRef As New AcadDb.AttributeReference

AttRef.SetPropertiesFrom(AttDef)
AttRef.SetAttributeFromBlock(AttDef, New AcadGty.Matrix3d)

BlockReference.AttributeCollection.AppendAttribute(AttRef)
Trans.AddNewlyCreatedDBObject(AttRef, True)
End If
Next

Trans.Commit()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
Trans.Abort()
Finally
Trans.Dispose()
End Try
End Sub

bweir
2008-01-28, 05:30 PM
Think I solved it. It seams that the text size and widths where not being set and received values of 0. This caused problems in AutoCAD as you can't have a height or width of 0. Looks to be working a lot better now. Now I just have a problem with the attribute reference not moving in my Jig. Any body have any ideas?

pauminku
2008-02-15, 10:31 PM
Hi, I tried your code, and have an "eWrongObjectType" exception in line:
If BlockReference.Database Is Nothing Then Block.Database.AddDBObject(BlockReference)
Please, can you explain what this statement do?

And also, please, can you publish the fixed code?

Thank-you very much in advance.

bweir
2008-02-16, 05:43 PM
Sorry Pauminku, I'll have to get back to you later. I'm just texting on my phone and don't have the code with me. I'll be back in my office on Tuesday, if I remember I'll post the update then.

Cheers!

pauminku
2008-02-20, 10:10 PM
Here is my code. The attributes will show up in the Property Palette and I can double click the block to edit the attributes but the attributes do not show in the drawing and sometimes when editing the attributes I get the error mentioned in the previous post.


I fixed it, you only have to change the line:

AttRef.SetAttributeFromBlock(AttDef, New AcadGty.Matrix3d)

for that one
AttRef.SetAttributeFromBlock(AttDef, BlockReference.BlockTransform)

Now, attributes have the same scale, position, etc. as the block reference.

I also supress the line:
If BlockReference.Database Is Nothing Then Block.Database.AddDBObject(BlockReference)

I think you only have to insert the block reference and be sure that the database knows it, with, for example: tm.AddNewlyCreatedDBObject(blocRef, True)