How C API works - What does the "Ref" mean in SUFaceRef?

I think I know the answer, but I should ask since I am about build a lot of code on my assumptions.

When I have a SUFaceRef object, in C++, I end up passing it to other functions quite often. I can pass it by value, or by reference. If I pass by value, I have a remote fear that I could end up with two objects, derived from the same object, but has the potential to diverge as it gets treated differently.

But then I think I am reassured by the “Ref” part of every C API object type. This indicates to me that every SUFaceRef is actually a reference to the Face object, and not the Face object itself. So in other words, after copying SUFaceRef, the “Reference” of the copied object still points to the original data.

So in other other words, the following two functions will always do the same thing to the same “object” (wherever it may be):

SUFaceRef add_loop_by_value(SUFaceRef face, SULoopInputRef loop) { // FaceRef is passed by value (copied)
// Initialise vertices here
SUFaceAddInnerLoop(face, vertices[0], &loop);
return face;
}
SUFaceRef add_loop_by_reference(SUFaceRef &face, SULoopInputRef loop) { // FaceRef is passed by reference (original copy passed)
// Initialise vertices here
SUFaceAddInnerLoop(face, vertices[0], &loop);
return face;
}

So in other other other words - I don’t need to worry about passing my C API objects by reference or by value in my C++ code? I can store multiple copies of a SUFaceRef object within other objects and they will all be working with/manipulating the same Face object?

Should I be reassured or gravely mistaken?

Yes, you are safe to copy the *Ref objects around. They are just a struct wrapping a pointer. You can see this in the headers in the SDK if you trace down the definitions of the symbols.

hi, you are developing a pligin in SU with C++ ? do you know how to create a button in SU by your plugin?

The sketchup C API (and C++ on the back of that) only deals with creating/ manipulating Sketchup files outside the application, and is generally intended for file import/export purposes.

You cannot use it to interact with the user interface within Sketchup itself. For that, you need to use the Ruby API (look up the Tools object for your situation). That said, there are developers who use Ruby API for user interactions, then use Ruby C Extensions to do the heavy lifting in C/C++.

1 Like

that is to say, if I want create a button in SU with C++ ,I can’t use C++ directly but to use ruby to draw button in SU with function developed by C++ ?

You can’t use C++ at all to create a button. You can only use Ruby:
http://www.sketchup.com/intl/en/developer/docs/ourdoc/toolbar

You CAN use C++ (with some effort) for doing some maths within a tool assigned to a button, but that is quite advanced stuff that I haven’t looked into much (look for Ruby C Extensions if you want to know more).

1 Like

A post was split to a new topic: SU API - how to exchange polygons into triangles by Triangulation

Hi @27925916

Oops nevermind - you want the C API equivalent.

It looks like you need to use a MeshHelper

http://extensions.sketchup.com/developer_center/sketchup_c_api/sketchup/mesh__helper_8h.html