A Correct Way of Importing skp files in a model

Hi, I’m using C API, and I need to import a model skp file into another model.

So I read a bunch of function definitions in the C API documentations page. And I end up with these example codes.

    SUModelRef = model = SU_INVALID;
    SUModelCreate(&model);
    SUEntitiesRef entities = SU_INVALID;
    SUModelGetEntities(model, &entities);
    SUComponentDefinitionRef component_definition = SU_INVALID;
    SUModelLoadStatus status;
    
    // Load the external SKP file as a component definition
    SUResult res = SUModelLoadDefinitionWithStatus(model, "wooden_door.skp", &component_definition, &status);
    if (res != SU_ERROR_NONE) {
        std::cerr << "Failed to load the external model << std::endl;
        return;
    }

    // Add the component definition to the model
    SUModelAddComponentDefinitions(model, 1, &component_definition);

    // Create an instance of the component definition (imported SKP file)
    SUComponentInstanceRef instance = SU_INVALID;
    SUComponentDefinitionCreateInstance(component_definition, &instance);
    
    // Apply the transformation matrix to the component instance
    SUTransformation transform = ... // some transformation initializing
    SUComponentInstanceSetTransform(instance, &transform);

    // Optionally, set a name for the component instance (not required)
    SUStringRef name = SU_INVALID;
    SUStringCreate(&name);
    SUEntitiesAddInstance(entities, instance, &name);
    SUStringRelease(&name);
    SUComponentInstanceRelease(&instance);
    SUComponentDefinitionRelease(&component_definition);

    SUModelSaveToFile(model, "output_model.skp");
    SUModelRelease(&model);
    SUTerminate();

However, I’m still struggling with this task because It doesn’t work. More specifically, the code runs with no error, but when I check the output_model.skp, There’s nothing.
I’m using Windows 11 and the latest SDK of C API.
Is this a correct way of importing skp files? Or is there any other way of doing it?

You should not be making an assignment to SUModelRef.

You mean the ‘SUModelRef = model’? It was just a typo and the actual code that I ran was, you know, ‘SUModelRef model = SU_INVALID;’

Then do you think the other part is normal?

Yes that.

Well, it looks as though it should work, but I’m not a C expert.

Have you tried with an older version of the SDK ?

I looked again, …

  1. You never checked the status after loading the definition from disk. You must use a valid absolute path to external files, I believe.

  2. Also, you never intialized the string name for the instance.

  3. I do not think you should be releasing the component instance and the definition before saving the model … as they are then “owned” by the model.
    According to the SketchUp C SDK main page (Memory Management) … any object added to the model or to an object belonging to the model “will be released automatically when the model is released.” So I think that you are actually deleting them when you release them just before the save.
    * IE, the SDK save routine likely has some internal safeguards that will not write invalid (unreferenced) objects out to the SKP file.