Get SUEntityRef from ruby in versions before 2020.2

I wonder if there is a way to get SUEntityRef from ruby in versions before 2020.2 .

What are you trying to do? (What is the scenario where you’re going between Ruby and C API?)

I’m going to get the changed entity from Observer in Ruby, and process the mesh info,material, etc in C API for better performace. I’m not sure if the same function in C API is faster, and I’m trying to use thread to boost.

Prior to 2020.2, from 2020.0 this could be done by getting the PID of the entity, passing that to C (I think as a string and converting it to a integer,) and then using the C API’s SUModelGetEntitiesByPersistentIDs() function. (If you look back at the old version of the Live C API Example repo [before the 2020.2 commit], you’ll see how this was done.)

I’m not sure how it was done before 2020. Perhaps pointers?

It does occur to me that you can use brute force iteration searches. For example attach a temporary attribute dictionary with a unique name (some devs use time stamp names) and get arrays of typed entities from the context (model or definition) you need to search. Then iterate the arrays looking for the entity “marked” with your attribute dictionary.

You can also choose to search for the entityID which is non-persistent but unique within the SketchUp session. This should be valid back to the beginning of the C API.

If you using 2018 or higher, the C API has a built-in entities array iterator interface. For older API versions you’d need to use base C or C++ array iteration.

For version prior to 2018, Jim Foltz posted an example of iterating an entity array at his API sandbox GitHub repo:
GitHub - jimfoltz/sketchup-c-api-sandbox: Tests and learning examples of using SketchUp's C API

1 Like

There’s a branch that show this being done:

1 Like

Have you done benchmarks and profiling? Did you get a significant performance boost by using the C API? (Assuming you tried the newer exchange API?)

SketchUp is not thread safe. Interactions with the SketchUp APIs should be done via the main thread. You might be lucky if you only read with the C API from different threads, but there’s no guaranty that some state isn’t in sync when doing that.

Before diving too deep into trying to get older SU versions to read from the model in the C API - do some benchmarks and profiling to be sure of where the bottlenecks are. The Ruby API is a thin layer on top of the SU C++ source. I’ve seen a number of cases where the performance gain over using the C API hasn’t been of any significant issue to the end user experience. Check that it’s actually the API layer is being the bottleneck and not SU itself before investing too much time into this.

1 Like