• Login
sketchucation logo sketchucation
  • Login
πŸ”Œ Quick Selection | Try Didier Bur's reworked classic extension that supercharges selections in SketchUp Download

Need an example to munipulate a dynamic component with ruby

Scheduled Pinned Locked Moved Developers' Forum
9 Posts 5 Posters 1.8k Views 5 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    shirazbj
    last edited by 27 Mar 2009, 02:55

    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

    1 Reply Last reply Reply Quote 0
    • H Offline
      HPW
      last edited by 29 Mar 2009, 07:16

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

      1 Reply Last reply Reply Quote 0
      • H Offline
        HPW
        last edited by 29 Mar 2009, 09:20

        Found some answers here:

        http://groups.google.com/group/sketchupruby/browse_frm/thread/eb7d2e7ab9038492/af73a7ee457b82df

        1 Reply Last reply Reply Quote 0
        • R Offline
          remus
          last edited by 29 Mar 2009, 16:52

          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.

          http://remusrendering.wordpress.com/

          1 Reply Last reply Reply Quote 0
          • S Offline
            shirazbj
            last edited by 30 Mar 2009, 10:09

            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.

            1 Reply Last reply Reply Quote 0
            • S Offline
              scottlininger
              last edited by 31 Mar 2009, 17:33

              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. πŸ˜‰

              • Scott Lininger
                SketchUp Software Engineer
                Have you visited the Ruby API Docs?
              1 Reply Last reply Reply Quote 0
              • C Offline
                Chris Fullmer
                last edited by 31 Mar 2009, 17:35

                ooh, I'm excited to check that out, thanks Scott!

                Chris

                Lately you've been tan, suspicious for the winter.
                All my Plugins I've written

                1 Reply Last reply Reply Quote 0
                • S Offline
                  shirazbj
                  last edited by 1 Apr 2009, 10:34

                  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 path1

                  point = Geom::Point3d.new 10,0,0
                  transform = Geom::Transformation.new point
                  instance=model.entities.add_instance componentdefinition1,transform

                  end

                  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

                  1 Reply Last reply Reply Quote 0
                  • S Offline
                    scottlininger
                    last edited by 1 Apr 2009, 14:57

                    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. πŸ˜„

                    • Scott Lininger
                      SketchUp Software Engineer
                      Have you visited the Ruby API Docs?
                    1 Reply Last reply Reply Quote 0
                    • 1 / 1
                    1 / 1
                    • First post
                      6/9
                      Last post
                    Buy SketchPlus
                    Buy SUbD
                    Buy WrapR
                    Buy eBook
                    Buy Modelur
                    Buy Vertex Tools
                    Buy SketchCuisine
                    Buy FormFonts

                    Advertisement