Skip to content

BlockReference Attribute Handling

davidbl edited this page Sep 13, 2010 · 1 revision

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:

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

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?)

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