Ruby Script Video Tutorial - The Menu System
-
This script takes the erase faces snippet and turns it into a full functioning ruby script. This includes adding it to the menu system, and wrapping it into a module.
[flash=1280,720:v7vjs0t1]http://www.youtube.com/v/YEJLw3WJsnU&hd=1&fs=1[/flash:v7vjs0t1]
-
Hi Chris, Thanks for the tut. I have a beginners question regarding modules. I read a little about the pitfalls of using $, @, and @@, in variable names, but without comprehensive understanding. Will the use of modules shield me from problems? I ask as I am a better hacker then a programmer, and I "borrowed" code (like mouse input) that I have little understanding of, and they sometimes contain these symbols. Any "rules of thumb" until I get a better hang of what I am doing?
Also is there any difference between collection.push e, collection.add e, or collection << e ?
-
I might be a little off (or maybe a lot!), but this is my understanding.
Array's are ruby objects. They are lists of objects in ruby.
A collection is not exactly a ruby object, but something refered to with some of the Sketchup objects. model.slection is a collection of entities. model.entities is a collection of entities. And I think they are only called collections because they are very much like an array, but they are more than an array. So you can't really call it an array.
For example, the three methods you are asking about -
.push
,<<
, and.add
. The first 2, .push and << are both regular ruby methods for regular ruby arrays. like this:a = [1,2,3,] a.push 4 a << 5
That is plain ruby. You can't use those methods on the entities object or the selection object because those are not actually arrays. But they each have their own special way of letting you add entities to them. the selection object uses the .add method. You can not use .add on a normal array. so this:
a = [1,2,3,] a.add 4
is not valid. But this is:
model = Sketchup.active_model ents = model.entities sel = model.selection sel.add ents
Also, try this:
` model = Sketchup.active_model
sel = model.selection
puts sel.classa=[1,2,3]
puts a.class`It will puts the class for the selection object. And it will puts the class for the a object, which is an array. Showing that it is important to realize that some of the SketchUp collections are not arrays at all. They just act similarly to an array. And if you want to return all the contents of the selection as an array, you can use sel.to_a, and you get it nicely returned as an actual array. However, at that point, you can no longer use .add on the array of entities, because it is not a true sxelection object. Like in this code:
model = Sketchup.active_model sel = model.selection ents = model.entities sel_array = sel.to_a puts sel.class puts sel_array.class sel.add ents sel_array.add ents
That last line will fail because .add is not a method available to arrays. Also, sel_array is not pointing to the SketchUp selection, it is pointint to an array that holds the entities that were in the selection. So even if you push entities into that array, they will not be added to the actual selection in SketchUp.
ok, I probably went overboard on all that. But it is important to understand the differences and why things work the way they do.
Chris
-
And the variable scope - there threads in the developer's section that might help more with understanding that. But the idea is that global variables start with the dollar sign like
$global
. Those are available across all of SketchUp. I can access the global variables set by any other script. And if 2 scripts try to set the same global variable, then one will win and one will lose. So if I need a global called $name that holds my name, and another script uses a global called $name to hold the name of their script, then in the end, the last script that loads will win.So using global variables is questionable, and mostly avoidable. I think the only place I use globals is in my menu system where I need to have my sub-menu object available to all my plugins. So I use a global variable to help all my plugins load into the same sub-menu.
The other 2 @ and @@ variables. Those only exist inside your class or module. So use those freely. @name in your class will not conflict with @name in my class. I don't have all the ins and outs of using those 2 variable scopes down yet, but in a class, the @@ variable is available to all objects of a class, whereas the @ is only available within each object of the class. This is helpful when you are making a SketchUp tool. It is common to make it so that each time your tool is run, you instantiate a new instance of your tool object. Something like this:
` class Clf_scope
code here
endSketchup.active_model.select_tool ( Clf_scope.new )`
Each time that tool is run, a new Clf_scope object is created. So say I have a user input option for a length of something, and I use this variable:
@length
It will only be available until the user de-activates the tool. Then when they re-activate the tool, it creates a new tool object and the @length variable, all @ scoped variables are not available. But if you name them with @@ scope, then they are available to all objects of your class. So the first time the user runs your tool, they will enter the @@length value. And when they use your script later, that @@length variable is still available to the next tool object.
I don't think I explained all that well..... but it is more or less accurate
Chris
-
Hi Chris, thanks for the clf_notes:-).
Advertisement