How to get face index instead of face object with C API?

Hey, there.

I want to get the adjacent faces sharing the specific edge by using SUEdgeGetFaces in C API.

However, the output of the function are face objects, what exactly I want are the indices of faces.

Did anybody do that before, need your help, really appreciate your time here!

The indices in relation to WHAT ?

Secondly, what are you assuming that the indices will indicate that would not change ?

Hi, Dan:

I wanna get the indices in relation to adjacent faces…

The mesh is imported from 3DSMax, and it’s triangle structure (all faces are constructed with three points). I use SUGeometryInputAddFace to add face into the SUGeometryInputRef, and then fill it into SUEntitiesRef with SUEntitiesFill.

I assume that index of face returned by SUGeometryInputAddFace would not change and be stored in somewhere. When I iterate over all edges in SUEntitiesRef, I could get the indices of faces associate with the specific edge.

I meant in relation to what collection. Your answer is the Entities collection.

I think that the index returned is an index in relation to your SUGeometryInputRef object’s temporary collection, not the model’s entities collection object. However if you add the geometry to a new empty group’s entities then I’d think perhaps that the indices may be retained.


You should be aware that none of the APIs give a contract that the indices within the entities collection is maintained in any particular order. The entities may be in added (creation) order initially, but later after housekeeping or after geometry is removed and edited, etc., the members of the collection may be reordered. You may find that the order might change after a save and reopening the model.

Now if you are getting indices just after having added the geometry and have not made any other changes then you could likely use indices temporarily. But the APIs were recently given persistent IDs that are meant for coders to use to “tag” entities.

Anyway, in Ruby the array (and enumerable mixin library) gives core language methods to get an index for a object in a collection. I would imagine C language also have this core language function that is not per se an API function.

At the worst, if you cannot find a core C array function, you’d just write up your own function to iterate the entities collection to find a match to the objects you pass in as a parameter, and break out returning the index when the match is true.

The simplified heart of the C code for Ruby’s Array#index() method is the following.
It is as I said a simple matching iterator wrapped up in a function …

static VALUE
get_index(int argc, VALUE *argv, VALUE ary)
{
    VALUE val;
    long i;

    if (argc == 0) {
        return Qnil;
    }
    rb_check_arity(argc, 0, 1);
    val = argv[0];

    for (i=0; i<RARRAY_LEN(ary); i++) {
        VALUE e = RARRAY_AREF(ary, i);
        if (rb_equal(e, val)) {
            return LONG2NUM(i);
        }
    }
    return Qnil;
}

This should give you an idea.


There are also these C API helpers …

1 Like

Lots of thanks, Dan.

I have completed my work according to your suggestion!

And it also lets me dig more about Sketchup C API. Appreciate for all.

1 Like