BlockReference Attribute Handling

I am currently testing a method that simplifies the editing of block reference attributes.

Given a simple block reference with attributes Name, Sex, Age, and Height, wouldn’t it be nice to be able to
edit the attributes like:

1
2
attribs.age = 45
attribs.name = "David"

So I hacked together a little method that uses my TransHelper class and an OpenStruct to store the attribute tags and values

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def get_block_ref(obj_id, mode=:Read)
    begin
        helper = TransHelper.new
        helper.trans([:Block])  do |tr, db, tables|
                ref = helper.get_obj obj_id, mode
                attrib_hash = {}
                ref.AttributeCollection.each do |id|
                        attrib = helper.get_obj id, :Read
                        attrib_hash[attrib.Tag.downcase] = attrib.TextString
                end
                @attribs = OpenStruct.new(attrib_hash)
                yield @attribs
                #update the attribs
                if mode == :Write
                        ref.AttributeCollection.each do |id|
                                attrib = helper.get_obj id, :Write
                                attrib.TextString = @attribs.send(attrib.Tag.downcase).to_s
                        end
                end        
            end    
        rescue Exception => e
                puts_ex e
        end
end

This little methods allows me to write an entire attribute editing method with very little coding. All that is really needed is some knowledge of the attribute tags (but surely we know the tag names if were are coding against the block, right?)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def edit_block_ref
        begin
          ref_id = get_entity_id #from AcadHelper to pick an entity on the screen
          get_block_ref(ref_id, :Write) do |attribs|
            attribs.age = 50  #numbers can be used - get_block_ref will convert to string.  YEA!
            attribs.name = "Chuck"
            attribs.height = "72 inches"
            attribs.sex = "Male"
           end
        rescue Exception => e
          puts_ex e
        end
end

I’m still testing this, but feel free to take if for a test ride.

When I complete the testing, I will add the method to AcadHelper

Looking for more? Head on over to the archives.

Post a comment


(lesstile enabled - surround code blocks with ---)

Pages

Github

Categories

There Must Be An Easier Way © David Blackmon. Valid XHTML and ATOM. Powered by Enki.