Problem adding polygon to mesh - again

FTR, I always convert to Geom::Point3d for large operations or when passing a bunch of points to API methods. I just works better. In the past we also had some issues with a few of the Entities add geometry methods.

Thanks, Dan. It also has the merit that I can use references to individual x, y, z values like:

points2[i] = [points1[i].x, y2, z2]

which is easier to understand than
points2[i] = [points1[i][0], y2, z2]

At some point you may want to check out the Array class. :wink:

Genuinely interested - doesn’t this just slow the script down with an unnecessary conversion which is done in the C++ code anyway?

I don’t care too much for speed usually.
Especially when not doing it gives an exception. No choice then.

As I said, we had problems where it was crashing in the C++. As if all that converting was being done recursively, and a stack overflow was happening. (Just guessing.)

Ah, yes - I see now. It’s the logic that handle the variable arguments. It checks if the first argument is an array - and if it is then it uses that as input args (even if you might have more arguments after that first array.)

So in the case of this method it probably is best to be explicit of using Geom::Point3d.

That said, for performance I always add the points first and build the polygons with indices to reduce the inefficient lookup for each point.

Only way to assertively test this is time it for your scenario. I’ve been surprised so many times of what is actually a performance impact and what is negligible.

That said, I worked a lot in optimising building geometry using the Ruby API and I don’t think I ever noticed any overhead of creating a Point3d over Array. In general in Ruby, creating objects comes with a cost, so you want to try to minimize that.

SketchUp have added some utility methods to Array: Class: Array — SketchUp Ruby API Documentation

Makes using arrays interchangeable with points and vectors more seamless.

That being said, for production code I often use Geom::Point3d and Geom::Vector3d for explicit code clarity. That way I document better what my intent is, by using an explicit class. I use the array shortcut when I’m thinkering in the Ruby Console.

I even create small subclasses only for the purpose of having a type hint in my code. (IDE can even help me).
For instance, there is no UV class in the Ruby API, but when you use face_position_material you have to provide 3d points and uvs. So I some times do stuff like this:

class UV < Array
end


def some_uv_method
  # ...
  uv_mapping = [
    Geom::Point3d.new(0, 0, 0),
    UV.new(0, 0),

    Geom::Point3d.new(9, 0, 0),
    UV.new(1, 0),

    Geom::Point3d.new(9, 9, 0),
    UV.new(1, 1),

    Geom::Point3d.new(0, 9, 0),
    UV.new(0, 1),
  ]
  face.position_material(material, uv_mapping, true)
end

That way the code explains the intent a bit better, instead of having just eight Point3d in a row. I have started doing the same for lines and planes - since the Ruby API doesn’t have dedicated classes for them either.