Need an example to munipulate a dynamic component with ruby
-
Hi,
After open a SKP file containing a dynamic component, I'd like to try to change the component options by ruby code. And place another instance.
Is it possible to do these under SU free version?
Thanks
-
@unknownuser said:
After open a SKP file containing a dynamic component, I'd like to try to change the component options by ruby code. And place another instance.
I am also interested in this question.
Furthermore I would like to know if it is possible to create a dynamic component at runtime with ruby code?
Or change it (Not only an option)? -
Found some answers here:
http://groups.google.com/group/sketchupruby/browse_frm/thread/eb7d2e7ab9038492/af73a7ee457b82df
-
If you send scottliniger a PM he might be able to knock up some example code, i know he's certainly talked about manipulating DCs with ruby before.
-
Thanks HPW.
The link you provided was last posted on Nov 25 2008.
I tried the code there. The skp files under "Components/Components sampler/" folder seem to be only a simple component or include dynamic component but no option.
I added an instance of an DC with option in that way, when i right click to try to change the option, I was told there is no option.
-
Hey guys,
Here's the "quick and dirty" overview of how to apply some options and then fire a redraw of a DC.
I'm working (slowly) on some better documentation. In the meantime, you can kind of understand how the various attributes work by inspecting the "dynamic_attributes" library yourself. This tool is handy for looking at attributes.
I just posted a tutorial on reading DC attributes to the code site. That could be helpful, too.

UI.menu("Plugins").add_item('Make Sang Red') { # Assumes that sang is the 1st entity in model. sang = Sketchup.active_model.entities[0] sang_def = sang.definition # Override sang's shirt color to red. ("material" # is a special attribute that requires # you to set a formula to "take control" # over the default material the user has painted.) sang_def.set_attribute 'dynamic_attributes', 'material', 'red' sang_def.set_attribute 'dynamic_attributes', '_material_formula', '"red"' # Add a new configurable option to Sang. # (Any attribute that starts with an underscore # is a "meta attribute" that describes behavior.) sang_def.set_attribute 'dynamic_attributes', 'weight', '145' sang_def.set_attribute 'dynamic_attributes', '_weight_label', 'weight' sang_def.set_attribute 'dynamic_attributes', '_weight_formlabel', 'My Weight' sang_def.set_attribute 'dynamic_attributes', '_weight_units', 'STRING' sang_def.set_attribute 'dynamic_attributes', '_weight_access', 'TEXTBOX' # Change the description that shows # up in the configure box with a custom # formula. sang_def.set_attribute 'dynamic_attributes', '_description_formula', '"Sang is now red and weighs " & weight' # There is a global handle into the plugin that # allows you to make the same calls that the # plugin does, like so... dcs = $dc_observers.get_latest_class dcs.redraw_with_undo(sang) }Let me know what doesn't make sense.

-
ooh, I'm excited to check that out, thanks Scott!
Chris
-
Thanks Scott. That's fantastic.
After try your code, I saved red Sang with weight option as a "red sang.skp" under folder "Components/Components Sampler/"
Then I tried to load this skp file like this:
def red_sang
path1=Sketchup.find_support_file "red Sang.skp" ,"Components/Components Sampler/"
model=Sketchup.active_model
definitions = model.definitions
componentdefinition1 = definitions.load path1point = Geom::Point3d.new 10,0,0
transform = Geom::Transformation.new point
instance=model.entities.add_instance componentdefinition1,transformend
UI.menu("Plugins").add_separator
UI.menu("Plugins").add_item("Draw red sang") { red_sang }But when I check the option, there is no option anymore.
Should I load the file in other way?
Thanks
-
When you saved redsang.skp, did you File > Save As, or did you Right click > Save as?
The File > Save As method is probably what happened. This isn't the same as saving sang as a standalone SKP. Try right clicking on red sang and "Save as" and then try the import. See if that works.

Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement