sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    First ruby script, please help..

    Scheduled Pinned Locked Moved Plugins
    9 Posts 3 Posters 547 Views 3 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.
    • liquid98L Offline
      liquid98
      last edited by

      Hi,

      This is my first script, its purpose is to scale everything in the model
      10 times.

      I looked around on the google api documentation, and downloaded some rubies
      to see how the structure of a rb file looks like . Until now I've come up with this:

      require 'sketchup'
      
      def scale
      	
       entities = Sketchup.active_model.entities
       selection = Sketchup.active_model.selection 
        
      
         trans = Geom;;Transformation.scaling 10
         UI.messagebox trans
        
         entities.transform_entities(trans) 
        
        end
        
        UI.menu("Plugins").add_item("scale with factor 10") {scale} 
      

      The first two lines in the code are for the selection of 'everything' in the model.
      Then the definition of the scaling action (trans=) and at last the execution of
      that action. (entities.transform)
      So in my beginners-eye everything is there, but sketchup tells me
      some weird hex-code in the messagebox.

      What am I doing wrong??

      Things that flourish fall into decay. This is not-Tao, And what is not-Tao soon ends ~ Lao tse

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        @liquid98 said:

        So in my beginners-eye everything is there, but sketchup tells me
        some weird hex-code in the messagebox.

        Please post that. The full error message is important to determine your problem.

        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • thomthomT Offline
          thomthom
          last edited by

          Was it something like this you where referring to: #<Geom::Transformation:0x180b93f8> ? That's what Geom::Transformation returns when you convert it to a string. If you want to inspect the matrix data, you can use .to_a

          Also, to avoid clashes with other plugins it's important to wrap your code in a module unique to you.

          <span class="syntaxdefault"><br />require </span><span class="syntaxstring">'sketchup'<br /><br /></span><span class="syntaxcomment"># Wrap your code in modules to avoid clashes with other plugins.<br /></span><span class="syntaxdefault">module Liquid98<br /><br />  def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">scale<br />    <br />    model </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />    <br />    </span><span class="syntaxcomment"># Use active_entities to ensure you get the entities of the current context.<br /></span><span class="syntaxdefault">    entities </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_entities<br />    selection </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selection<br /><br /><br />    trans </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Geom</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Transformation</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">scaling 10<br />    UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">messagebox trans</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_a </span><span class="syntaxcomment"># You can use .to_a to inspect the Transformation matrix<br /><br /></span><span class="syntaxdefault">    entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transform_entities</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">trans</span><span class="syntaxkeyword">)<br /><br /></span><span class="syntaxdefault">  end<br />  <br />  </span><span class="syntaxcomment"># Prevent adding the menu items more than once when reloading the script.<br /></span><span class="syntaxdefault">  unless file_loaded</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault"> __FILE__ </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">    UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Plugins"</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"scale with factor 10"</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">scale </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">  end<br />  <br /><br />end </span><span class="syntaxcomment"># module<br /><br /></span><span class="syntaxdefault">file_loaded</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> __FILE__ </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span>
          

          Thomas Thomassen — SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            I think that
            entities.transform_entities(trans)
            needs to be
            entities.transform_entities(trans, selection.to_a)
            otherwise you are NOT telling it what to transform...........

            TIG

            1 Reply Last reply Reply Quote 0
            • liquid98L Offline
              liquid98
              last edited by

              @Thom Thom: I understand all your recommendations and will use them in the future! Tnx
              @ TIG: When I added this to the code: entities.transform_entities(trans, selection.to_a) the script worked.

              I've read about to_a but its says something about "The to_a method retrieves a 16 element array which contains the values that define the Transformation." What's that ??

              By the way TIG, thank you as well

              In general I think http://code.google.com/intl/nl/apis/sketchup/docs/index.html is too abstract for me,
              Where can I find some simple example scripts to start with even simpler than the example-scripts in the plugins dir?

              Liquid

              Things that flourish fall into decay. This is not-Tao, And what is not-Tao soon ends ~ Lao tse

              1 Reply Last reply Reply Quote 0
              • TIGT Offline
                TIG Moderator
                last edited by

                The ' .to_a' applies to several Sketchup objects and the one you quote about 16 item array is related to a transformation that can be made into any array so you can get or change one element individually.
                An entities object acts rather like an array but it changes dynamically and always returns a list of every entity in entities collection - therefore you can do this like modify an entities set en mass of get a particular entity in the entities BUT if you use an entities object i a situation where an iteration is changing what the entities it self contains then all sorts of weirdness can happen... So entities.to_a takes a 'snapshot' of the entities collect and makes it into a proper array so then when you refer to it it no longer changes if an entity is removed from or added to the entities collect - it's the list of the current entities at the instant you use .to_a on the entities...
                Similarly the selection is a special kind of list always returning what is currently selected BUT you can again free a version of it into an array using selection.to_array...
                Some methods require you to pass 'entities' - i.e. one or more entity as a comma separated list or an array - they do not take an object like entities or selection directly without you using the .to_a method on them first... 🤓

                TIG

                1 Reply Last reply Reply Quote 0
                • liquid98L Offline
                  liquid98
                  last edited by

                  TIG: There are some tiny tiny hints of understanding in my right hemisphere right now, but that's about it..
                  But my first script is a working script now, thanx to you guys! ☀
                  (First_Script.working=yes(!)

                  Again I want to ask you to point me to some really simple step by step examples besides the
                  standard google stuff.. please?

                  Things that flourish fall into decay. This is not-Tao, And what is not-Tao soon ends ~ Lao tse

                  1 Reply Last reply Reply Quote 0
                  • TIGT Offline
                    TIG Moderator
                    last edited by

                    Chris Fullmer did some simple script examples in Tutorials section...

                    TIG

                    1 Reply Last reply Reply Quote 0
                    • liquid98L Offline
                      liquid98
                      last edited by

                      Ok I'll start from there.. 😛

                      Things that flourish fall into decay. This is not-Tao, And what is not-Tao soon ends ~ Lao tse

                      1 Reply Last reply Reply Quote 0
                      • 1 / 1
                      • First post
                        Last post
                      Buy SketchPlus
                      Buy SUbD
                      Buy WrapR
                      Buy eBook
                      Buy Modelur
                      Buy Vertex Tools
                      Buy SketchCuisine
                      Buy FormFonts

                      Advertisement