How to add a new style to model in SDK?

Hello everybody:
I have a question about how to add new style, I use
SUModelGetStyles(model, &styles);
SUResult res = SUStylesAddStyle(styles, “D:/new.style”, true);The style is saved by sketchup, it’s name is “MyTest”,But the res‘s return value is SU_ERROR_DUPLICATE. what’s wrong with the code?

Although I’ve never explored the SDK and my programming is - let’s just say “rusty” (and that’s an understatement), I have to ask:

Is there any chance the model already contains the style you’re trying to add? If so, then that’s what the error return value is telling you - you’re trying to add a style that’s already in the model.

My model is create by sdk, so it cannot contain my style that I want to add;
like this:

#include <SketchUpAPI/slapi.h>
#include <SketchUpAPI/geometry.h>
#include <SketchUpAPI/initialize.h>
#include <SketchUpAPI/model/model.h>
#include <SketchUpAPI/model/entities.h>
#include <SketchUpAPI/model/face.h>
#include <SketchUpAPI/model/edge.h>
#include <SketchUpAPI/model/vertex.h>
#include <SketchUpAPI/model/styles.h>
#include <SketchUpAPI/model/style.h>
#include <vector>

int main() {
	// Always initialize the API before using it
	SUInitialize();
	// Create an empty model
	SUModelRef model = SU_INVALID;
	SUResult res = SUModelCreate(&model);
	// It's best to always check the return code from each SU function call.
	// Only showing this check once to keep this example short.
	if (res != SU_ERROR_NONE)
		return 1;
	// Get the entity container of the model
	SUEntitiesRef entities = SU_INVALID;
	SUModelGetEntities(model, &entities);
	// Create a loop input describing the vertex ordering for a face's outer loop
	SULoopInputRef outer_loop = SU_INVALID;
	SULoopInputCreate(&outer_loop);
	for (size_t i = 0; i < 4; ++i) {
		SULoopInputAddVertexIndex(outer_loop, i);
	}
	// Create the face
	SUFaceRef face = SU_INVALID;
	SUPoint3D vertices[4] = { { 0,   0,   0 },
	{ 100, 100, 0 },
	{ 100, 100, 100 },
	{ 0,   0,   100 } };
	SUFaceCreate(&face, vertices, &outer_loop);
	// Add the face to the entities
	SUEntitiesAddFaces(entities, 1, &face);
	SUStylesRef styles = SU_INVALID;
	SUModelGetStyles(model, &styles);
	SUResult res2 = SUStylesAddStyle(styles, "d:/watermark.style", true);
	if(res2 == SU_ERROR_DUPLICATE){
		printf("failed");
		getchar();
	}
	// Save the in-memory model to a file
	SUModelSaveToFile(model, "new_model2.skp");
	// Must release the model or there will be memory leaks
	SUModelRelease(&model);
	// Always terminate the API when done using it
	SUTerminate();
	return 0;
}

OK. As I said, I know nothing of the SDK. Just thought the possibility should be raised.

I leave it to other, more current programmers who know the SDK to help you find the problem.

ok,thanks

Just guessing but, do you need to first call …

SUStyleCreateFromFile (SUStyleRef *style, const char *path)

… before …

SUStylesAddStyle (SUStylesRef styles, const char *path, bool activate)

:question:

I tried to add this code, but it didn’t work,is this a bug?

Hi,

Thanks for bringing this issue up. Could you please try using SUStyleCreateFromFile and verify whether you are getting the return value of SU_ERROR_SERIALIZATION?

I suspect that your .style file is failing to load for some reason. I did find an error in the API for SUStylesAddStyle where it would incorrectly return SU_ERROR_DUPLICATE if a style couldn’t be created from the path provided. We will look into addressing this unhelpful error code for the next release of the API.

Thanks,
Adam

2 Likes

Hi,
I’ve already tested it, and the return value is SU_ERROR_SERIALIZATION ,May I ask when the next version will be released?
Thanks
Yun

Hi,
I download “SDK_WIN_x64_2019-0-753_0” to make it, but SUStyleCreateFromFile return SU_ERROR_SERIALIZATION

We’ve identified a bug that can cause .style files to fail loading when using the C API. It’s been logged in our internal bug tracker.

1 Like

C API - API Version 7.1 - SketchUp 2019.2

1 Like