Need guidance on surface area script

Okay, the zeroth thing to learn is how to post code blocks in the forum using markdown:

Second I compiled a set Ruby Learning Resource list here in a pinned topic thread …

Just a few points, … this references the model’s entities collection. (Things that are created and managed in the core C++ like the model and it’s collections, do not have constructor methods exposed as we would never need to “create” them.)

And (2nd) this means only at the top level.

Some how the API doc overview for this class got changed as I think I remember it being more descriptive and informative (ie, that component definition’s for instances and groups also have an entities collection.)
Within the past week I logged a documentation issue about this in the official tracker.

So, if you only iterate this collection your snippet references, then you’ll only get materials that a user painted at the top level, and then only the “instance / group” wrapper material assignments.

Often users need to open groups and paint the faces directly because of UV mapping features. (Especially for wood grain textures. etc.)

So you may find yourself needing to check each instance of used definitions, and check which faces within it’s entities are directly painted, and which ones still have nil assigned (which mean to inherit the instance wrapper’s material assignment.)

Don’t assume. Most SketchUp collections are exposed to Ruby with Ruby’s core Enumerable library module mixed in. From this library comes the #grep method …

model = Sketchup.active_model
comps = model.entities.grep(Sketchup::ComponentInstance)
groups = model.entities.grep(Sketchup::Group)

This method is very fast because it does class identity comparison.

The attribute dictionaries are attached to Sketchup::Entity API objects in the model database.

This would modify the model’s objects as code is iterating it’s collections. This is not necessary, and the amounts can quickly become incorrect (ie, out of sync with the model when edited.)

If you are looking for temporary memory storage of a hierarchical nature then Ruby’s core Hash class is very similar to the SketchUp API’s dictionaries (which come from the C++ class of the same name.)
Hash class objects are more powerful than the SketchUp API dictionaries. For example the #to_json method added by the JSON library can create a data string in JSON format easily output to a text file object.

The Ruby Standard Library has the CSV library. You can use.

But if your text lines are going to be rather simple, there are other simple core class methods of Array and String classes that can easily stitch text data together.

Ie, … say you have an array line1 whose members are strings that you wish to join together separated by commas.

csv1 = line1.join(',')

Say you have a nested array lines of line arrays (that you wish to join with commas) and join together with newlines, into one CSV string (so you can write it to a file object.)

csv_text = lines.map {|line| line.join(',') }.join("\n")

This looks weird but it is shorthand for …

# Create a new array whose items are csv text from the nested data arrays:
joined_csv_lines = lines.map {|line| line.join(',') }
# Now join the outer array into one big string (newline at end of each line):
csv_text = joined_csv_lines.join("\n")
1 Like