Sketchup C API how to add new layers to a model

I’m currently working with the sketchup C APi on a little software that would allow me to automatize the creation of several layouts on basis of a .skp file representing a wooden house.

So I use SUModelCreateFromFile (everything is fine from here) and then i want to create new layers to add to the model, but I quite don’t get how to do it.

Let’s say for example that I would like to create a layer allowing to toggle all the outer panels of the house. I Can make a std::vector containing all the component instances I need (works fine from here), but then what?

I’ve tried to create a group containing all these component instances, but i can’t add the instances to the entities of this group. (i.e. SUEntitiesAddInstance returns an error)

I’ve also tried to create one layer per panel, using SUComponentInstanceToEntity and then SULayerFromEntity, but the SUModelAddLayers won’t work…

I’m quite confused. I can provide you with some piece of my code if this can be of any help.

Layers, Entities and tags ... Oh my! (click to expand) ...

A Layer tag is not the same kind of animal as an instance or any DrawingElement for that matter.

Basically for both APIs (Ruby and C) only objects of DrawingElement subclasses can be added to an Entities collection.

A layer/tag is an Entity subclass, meaning it is a member of a collection that belongs to the Model database, but it is a member of the Layers collection class, not the Entities class.

If your new to SketchUp, you need to know that in the GUI, the name Layer has been changed to Tag. (But the APIs will not change the name of the class. This is also why Scene/Scenes is still called Page/Pages in the APIs.)

Anyway, in SketchUp a layer/tag is a property that DrawingElement subclass objects are “tagged” with, so that the rendering engine can know how (color) and whether or not (visibility) to display them. Ie, a layer/tag is a set of display properties, that multiple objects share regardless of where in the model entity hierarchy these objects may be.

It does not matter at what nesting level a door is, or what branch of the entity tree a door exists in, all doors can be tagged as a door and toggled off, or shown in the same color if Color By Layer/Tag is on.

So by now you should get the idea that a layer/tag is not a geometric context or container. (New users were confused by the terminology thinking that layers should contain objects and separate geometry. But SketchUp uses group and component instances as geometric containers instead.)

Good morning Dan,

First of all sorry for the reply I’ve sent on the other topic. I’m new to this forum, but I got this rule now.

Thanks for your fast answer. I guess I now have the necessary information to work it out. I’ll let you know.

Please delete the other off-topic post.

Most functions return codes. The docs tell you what the codes are. What return code did you get?

Also post a snippet showing what the code is that doesn’t work. Read this how to post code:

Thank you so much for your help!

All I needed was to cast groups or componentInstances to drawing elements, then create my layers, use SUDrawingElementSetLayer to set the tags and finally use SUModelAddLayers.

Looks completely childish now that i got it, but i’ve spent several days getting crazy with that problem.

See you, thanks again!

1 Like

You will find that it matters what order you add things. Generally after creating objects, add then to a model or one of it’s collections before you use it, and especially before you begin setting or changing it’s properties.

So I’d think it’d be safer to add the layers to the model’s layers collection, and add the drawingelement to some entities collection (that is already properly attached to the model or a definition that is already a member of the model’s definition list collection,) before layer-tagging any elements.