Ruby Plugin Help
-
Hello,
I've made a a plugin for Ruby before, but I'm writing another one and it requires something a bit tougher...

I want to gather all the groups that have the name that starts with "SPECIAL". In reality these groups will contain no faces, but simply curves. I want to get each "point" in the group and write it to a file (I know how to do that).
Now question is, how do I get the points for a line segment?
If I need to clarify more simply ask,
Thanks,
Alex
-
verts=[]
group.entities.each{|e| verts << e.vertices if e.class==Sketchup::Edge }
verts.flatten!each e.vertices returns a two element array, flatten explodes these
inner arrays so that verts is then one array...
verts.uniq!
this removes duplicate vertices from verts, i.e. because in a curve
most vertices get listed twice because they will have two edges...
points=[]
verts.each{|v| points << v.position }points is now a list of all points represented by
all of the edges' vertices inside the given group...
-
Thanks, TIG!
How can I separate the groups whos names start with SPECIAL and the ones that don't, though? Thanks for your help,
Alex
EDIT: And also, I'm trying to use the 'puts' function, but when I'm doing this: file.puts "<model/>", it thinks that when I use the < & > symbols that I'm trying to define a constant or something. This is the same for the 'print' function. How can I add the < > characters? Thanks for your patience with me.

-
Well since you use the group.name= method to set the group's name, you will use the group.name method to retrieve it. Use a comparison expression:
verts=[
Sketchup.active_model.entities.each {|ent|
if ent.kind_of?(Sketchup::Group) && ent.name == 'SPECIAL']... then iterate the group's entites ...
... then flatten and uniqify the verts array,
... which can be done in one line:
%(#BF0000)[verts.flatten!.uniq!end # if SPECIAL group
}
points=[]]
... rest of code as shown above. -
Sorry I wasn't clear enough... I mean to retrieve whatever groups whos name STARTS with SPECIAL... Is there is an equivalent to C's "StartsWith" in Ruby?
-
You test for substrings using .include?
if ent.kind_of?(Sketchup::Group) && ent.name.include?('_SPECIAL_')If you wish to really use starts with, then you can test a subrange of a string:
if ent.kind_of?(Sketchup::Group) && ent.name[0..8] == '_SPECIAL_'or:
if ent.kind_of?(Sketchup::Group) && ent.name[0,9] == '_SPECIAL_'
You can look up Core methods online:
http://www.ruby-doc.org/core-1.8.6/index.html.. or download the CHM for offline use, from my post here:
http://forums.sketchucation.com/viewtopic.php?f=180&t=10142&p=266725#p266725 -
@alexs said:
Sorry I wasn't clear enough... I mean to retrieve whatever groups whos name STARTS with SPECIAL... Is there is an equivalent to C's "StartsWith" in Ruby?
You can use RegEx:
my_string.test(/^_SPECIAL_/)Or simple text operation:
my_string[0,9] == '_SPECIAL_' -
To explain a little more about 'patterns'...
/^_SPECIAL_/
The ^ at the start says 'starts_with'
A $ at the end says 'ends_with'
/_SPECIAL_$/
Using both is equivalent to 'is_equal_to'.
Using neither says 'contains_this_text_anywhere' in the string...Note that the my_string.test(//) method is equivalent to my_string =~ // that you might also come across...
The whole pattern thing can be quite complex - e.g.
/^[^a-z0-9]/
says starts with any character (^ inside the [] reverses the match) that is NOT a lowercase letter or number.
With gsub you can use something like it to 'squeeze' everything out of say a file-name so it only uses letters, numbers, '-' or '_' - e.g. as used in OBJ-file naming conventions, thus:
filename.gsub!(/[^-_A-Za-z0-8]/, '_')There are several websites that cover this in considerable detail.......
-
Thank you guys, you've been a great help.
Now... just one LAST question. I'm trying to use the 'print' and 'puts' function, and I'm trying to put in these characters: < >
But when I load Sketchup, it gives me an error. I guess it thinks I'm trying to do something within the string when I put a < or >.
How can I fix this?
-
Those characters are both Ruby operators and instance method names.
Use the String concate method + to add those chars (which must be quoted.)
my_string = 'Some special string' + ' > ' + 'the last part of the string.'or you can use replacement within double-quoted string:
my_string = "The value: #{biggernum.to_s} > #{smallernum.to_s}"
wherebiggernumandsmallernumare numeric references.The 3rd way is to use the Integer instance method chr if you know the character's ordinal within the UTF-8 set.
Since decimal 60 is the ordinal for "less than" ...
60.chrreturns the single char string"<"
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