Editing string variables
-
If I have a variable "mat" that is a string "FileName.Dxf", how do I edit the variable to remove ".Dxf"
unless mat.nil? if e.is_a? Sketchup;;ComponentInstance or e.is_a? Sketchup;;Group then #String editing to remove ".Dxf" here e.material = mat end endDoes my question make sense?
-
For files there's a separate method for that:
File.basenameunless mat.nil? if e.is_a? Sketchup;;ComponentInstance or e.is_a? Sketchup;;Group then #String editing to remove ".Dxf" here mat = File.basename(mat, ".Dxf") e.material = mat end end -
Alternatively, if the file type isn't always known:
unless mat.nil? if e.is_a? Sketchup;;ComponentInstance or e.is_a? Sketchup;;Group then #String editing to remove ".Dxf" here mat = mat.split('.').first e.material = mat end end -
Thanks, I am coding a plugin that allows me to texture imported components (dxf files) by applying a texture of a similar name to the imported component's name. In other words a imported component named "roof.dxf", will be textured by a material named "roof".
-
Does it work to set
entity.material = "stringName"? -
Thomas, This is my first real attempt to understand ruby, so far its not working. Thanks for the help, I'll get it eventually.
-
WooPee!! Got it. Ruby is case sensitive, and I was attempting the string conversion in the wrong place.
# Loop through entities & apply material based on layer name Sketchup.active_model.entities.each do |e| begin#if here found #10/30/09 ignore file extention name start stuff=e.definition.name stuff=stuff.split('.').first mat = su_materials[stuff] #mat = su_materials[e.definition.name]#original #end rescue#else if here not found mat = su_materials[e.name] end#end ifCan't get over not having to declare variables. Dangerous for guys like me.
-
The general way to do string fiddling in Ruby is via RegEx and it's not that hard:
x = 'asdf.wer' # match string to regex; string =~ regex # regex; '/'...'/' # match into $1 and $2; string =~ /(...)(...)/ x =~ /(.*)\.(.*)/ # match (any number of any char)a period(more chars) puts $1 # 'asdf' puts $2 # 'wer' -
Plus, regex can handle the Unicode data from Sketchup, which SU's Ruby doesn't.
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