RUBY: how to change a DC attrribute value of an instance


#1

Hello, I have a ruby script working that puts a DC instance in a model. The DC name is “raam01”. How can I change the value for instance attribute “lenX”? Thank you.


#2

Dynamic component attributes and values are generated through the use of SketchUp attribute libraries.
You can use various extensions from the Extension Warehouse to view the structure of these attribute libraries and their values.

Hope this helps
CD


#3

Related. For finding the DC instance, see this other post in this category:


#4

Thank you for the responses, however I’m not able to do what I really want. I want to change the value of lenx of both DC instances (see screenshot) in a ruby script.
Would there be a sample script I can build on? Thank you so much!


#5

When you have your dynamic component selected you can run this in the ruby console.
It should show you all the attributes you are looking for.

selection.each{ |ent|
dict = ent.attribute_dictionary(‘dynamic_attributes’)
dict.each { | key, val |
puts “#{key}:#{val}”
}
}


#6

I collected a few posts from the old Groups forum which may provide some insight.


#7

Thanks, however it returns an error:

      Error: #<NameError: undefined local variable or method `selection' for main:Object>
    <main>:in `<main>'
    SketchUp:1:in `eval'

Not sure that I need the list, I only want to change the value lenx from 80 cm to a different value in a ruby script/extension.

Really appreciate you help!


#8

Consider something like:

def dc_redraw(ent, pbar_visible=true, undo=false)
    begin
        ldc=$dc_observers.get_latest_class()
        if undo
            ldc.method(:redraw_with_undo).call(ent, pbar_visible)
        else
            ldc.determine_movetool_behaviors(ent)
            DCProgressBar::clear()
            ldc.method(:redraw).call(ent, pbar_visible)
            DCProgressBar::clear()
            ldc.refresh_dialogs()
        end
    rescue TypeError => e
        p e
    end
    # suppresses nil to float conversion error that happens
    # when redraw is called directly with true 2nd arg ?
    ###
    @view.refresh
    return true ### to slow nested commit_operation etc
end#def
###
### It's NOT: $dc_observers.get_latest_class.redraw_with_undo(ent)
### It's:     self.dc_redraw(dc, true, false)


### Then in later CODE, USE say...
dc = Sketchup.active_model.selection[0]
### assuming it's a selected instance, we need to process its definition ...
x = 80.cm ### say
dc.set_attribute('dynamic_attributes', 'x_len', x.to_s)
until dc_redraw(dc, true, false); end
### It's now updated: do something else ?
###

#9

You will realize that often examples expect some objects to be already referenced.

In your case it is obvious that beforehand selection needs to be defined:

selection = Sketchup.active_model.selection

#10

Thank you gentlemen, you’ve put me in the right direction. My SketchUp extension is now able to use inputbox data to place dynamic components on the right coordinates, width and height.