Set custom_text in layout ruby

Greetings, I can’t figure out the basic example. As far as I understand it, it takes the index of the property and writes a value on it. I tried getting a list of all the indexes, and then using a primitive if else filter to fill it with the value. I ended up with an error.

"undefined method `at_index' for #<Layout::AutoTextDefinitions:0x000002070bca2a10>\nDid you mean? index (Line 15)"
Error: #<TypeError: no implicit conversion of NoMethodError into String

Sample code, only for running in console:

# Default code, use or delete...
SKETCHUP_CONSOLE.clear
doc = Layout::Document.open("C:/layout/ot.layout")
doc.auto_text_definitions.add("Test", Layout::AutoTextDefinition::TYPE_CUSTOM_TEXT);
auto_texts = doc.auto_text_definitions

auto_texts.each { |auto_text|
  if auto_text.tag == "<Test>"
    at_index = auto_texts.index(auto_text)
    auto_texts.at_index.custom_text = "Boop"
    puts "#{auto_text.tag} - #{auto_text} - {at_index.custom_text} - #{at_index}"
    else
    puts "#{auto_text.tag} - #{auto_texts.index(auto_text)}"
  end
}

What am I doing wrong or where can I see a more complete example of filling values.
I want to generate a file based on a template that specifies a set of autotext tags.

Figured it out, I was getting too into the basic example. In my case, I should have done it head-on. Since I’m iterating through all the values in the iterator, already after creating them.

So in my case, the construction was primitive.

auto_texts.each { |at|
  at_index = auto_texts.index(at)
  if at.tag == "<Test>"    
    at.custom_text = "FooBAR"
    puts "#{at.tag} - #{at} - #{at_index} - #{at.custom_text}"
    else

  end
}

In the 1st post your error was here …

auto_texts.at_index.custom_text = "Boop"

… because class Layout::AutoTextDefinitions (ie, auto_texts) does not have an #at_index method, either by definition nor including module Enumerable.

The actual method to access the collection by index is Layout::AutoTextDefinitions#[], so …

auto_texts[at_index].custom_text = "Boop"

If you need to use the index of an item from one of the SketchUp or LayOut API’s collections, try to remember that the mixin module Enumerable has added many nifty methods.

One of those you will find yourself using quite often will be #each_with_index.

auto_texts.each_with_index { |auto_text, index|
  if auto_text.tag == "<Test>"    
    auto_text.custom_text = "FooBAR"
    puts "#{auto_text.tag} - #{auto_text} - #{index} - #{auto_text.custom_text}"
  else
    puts "There was a boo boo!"
  end
}
1 Like