First ruby script, please help..
-
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??
-
@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.
-
Was it something like this you where referring to:
#<Geom::Transformation:0x180b93f8>
? That's whatGeom::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>
-
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........... -
@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
-
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.
Anentities
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... Soentities.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 theselection
is a special kind of list always returning what is currently selected BUT you can again free a version of it into an array usingselection.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 likeentities
orselection
directly without you using the.to_a
method on them first... -
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? -
Chris Fullmer did some simple script examples in Tutorials section...
-
Ok I'll start from there..
Advertisement