What are the details of your problem? I'm working on a Ruby script for SketchUp to automate the process of adding text annotations within my model. I need to count instances of specific components ("Enkeltreol" and "Dobbeltreol") and display their totals as text within the model. I'm attempting to use the add_3d_text method for this purpose, but I keep running into a TypeError that I can't resolve.
What did you try and what were you expecting? I've tried using the add_3d_text method to create the text annotations, but the script either doesn't produce text in the model, or it results in a TypeError. I expected to see a text annotation showing the count of my components placed at a specific point in the model. Instead, the console reports an error, and no text appears.
I'm using SketchUp Pro Version 23.1.341.
Questions:
1: What is the correct way to use add_3d_text in the SketchUp Ruby API for adding dynamic text annotations to a model?
2: How can I resolve the TypeError and ensure the text is correctly added to the model at a specific point?
3: Are there alternative approaches within the SketchUp Ruby API for dynamically displaying text information based on model components?
Any insights or examples of correctly implementing dynamic text annotations in SketchUp using Ruby would be greatly appreciated.
Here is an example of the code i have been trying to run:
# Initialize counters
enkeltreol_count = 0
dobbeltreol_count = 0
# Start an operation so it can be undone in one step if needed
model = Sketchup.active_model
model.start_operation('Add Count Text', true)
# Iterate through all entities in the model for counting
definitions = model.definitions
definitions.each do |definition|
definition.instances.each do |instance|
if instance.definition.name == "Enkeltreol"
enkeltreol_count += 1
elsif instance.definition.name == "Dobbeltreol"
dobbeltreol_count += 1
end
end
end
# Calculate the total number of shelf units
total_shelf_units = enkeltreol_count + (dobbeltreol_count * 2)
# Create a text string that includes the counts
count_text = "Enkeltreol units: #{enkeltreol_count}\nDobbeltreol units: #{dobbeltreol_count}\nTotal shelf units: #{total_shelf_units}"
# Define parameters for the 3D text
font = "Arial"
bold = false
italic = false
height = 10.0 # Height of the text
z = 0.0 # Extrusion depth of the 3D text
# Define the point where the 3D text will be inserted
point = Geom::Point3d.new(10, 0, 0)
# Correctly add the 3D text to the model, placed flat like a label
entities = model.active_entities
entities.add_3d_text(count_text, TextAlignLeft, font, bold, italic, height, z, false, point)
# Commit the operation
model.commit_operation
# Output to the console
puts "Counts have been added to the model as 3D text."
Output
Output to the console
puts "Counts have been added to the model as 3D text." Error: #<TypeError: no implicit conversion to float from false>
:38:in `add_3d_text' :38:in `' SketchUp:in `eval' => nil