How to lock a SUGroupRef using C++ API

I am new to skecthup, I wanted to know if it is currently possible to lock a group using 2016 C++ SDK similar to what it is possible to do with Ruby SDK :

Add a group to the model using Ruby code.

group = Sketchup.active_model.entities.add_group
group.entities.add_line([0,0,0],[100,100,100])
status = group.locked = true

Thanks for your help.

Cast your SUGroupRef to and SUComponentInstanceRef and use SUComponentInstanceSetLocked
http://extensions.sketchup.com/developer_center/sketchup_c_api/sketchup/component__instance_8h.html#a4de9d436792313a8a96899ce83d9d9d7

Thanks for your quick answer, how would you cast SUGroupRef to an SUComponentInstanceRef, do you use static_cast or there an skp api function?

Thanks for your help.

SUGroupToComponentInstance
http://extensions.sketchup.com/developer_center/sketchup_c_api/sketchup/group_8h.html#ac7c5d6dec08538a51b86a61f4aa66066

I have tested the following but it does not lock the group (I don’t have the blue box around the element when I select it)

Here is the piece of code I have used :

{		
	//Get number of instance
	SUComponentInstanceRef instance = SU_INVALID;

	instance = SUGroupToComponentInstance( mGroup );

	ASSERT( SUIsValid( instance ) ) ;
	
	SU_CALL( SUComponentInstanceSetLocked( instance, lock ) );

}

‘lock’ is true at execution time.

Am I missing something?

I am not able to reproduce this. The functions locks the group as expected in my test.

You say “blue” - the default style in SU display a red boundingbox when you select a locked instance. However this is style dependant. Please check the Entity Info.

If you still see it doesn’t working, please provide a full example that reproduce the issue. Including any models that might be needed.

Here is the test snipped I used:

// SLAPI-Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <array>
#include <assert.h>
#include <iostream>
#include <vector>

#include "SketchUpAPI/sketchup.h"


int _tmain(int argc, _TCHAR* argv[])
{
  SUInitialize();

  SUModelRef model = SU_INVALID;
  SUResult result = SUModelCreateFromFile(&model, "C:\\Users\\tthomas2\\SourceTree\\SLAPI-Test\\models\\SU-31417.skp");
  assert(result == SU_ERROR_NONE);

  SUEntitiesRef entities = SU_INVALID;
  result = SUModelGetEntities(model, &entities);
  assert(result == SU_ERROR_NONE);

  size_t num_groups = 0;
  result = SUEntitiesGetNumGroups(entities, &num_groups);
  assert(result == SU_ERROR_NONE);

  std::vector<SUGroupRef> groups(num_groups, SU_INVALID);
  size_t num_group_out = 0;
  result = SUEntitiesGetGroups(entities, num_groups, &groups[0], &num_group_out);
  assert(result == SU_ERROR_NONE);

  SUGroupRef group = groups.back();

  SUEntitiesRef group_entities = SU_INVALID;
  result = SUGroupGetEntities(group, &group_entities);
  assert(result == SU_ERROR_NONE);

  // SUGroupToComponentInstance
  SUComponentInstanceRef instance = SU_INVALID;
  instance = SUGroupToComponentInstance(group);
  assert(SUIsValid(instance));

  // SUGroupFromComponentInstance
  SUGroupRef group2 = SUGroupFromComponentInstance(instance);
  assert(SUIsValid(group2));

  // SUComponentInstanceIsLocked
  bool is_locked = false;
  result = SUComponentInstanceIsLocked(instance, &is_locked);
  assert(result == SU_ERROR_NONE);
  assert(!is_locked);

  // SUComponentInstanceSetLocked
  result = SUComponentInstanceSetLocked(instance, true);
  assert(result == SU_ERROR_NONE);

  result = SUComponentInstanceIsLocked(instance, &is_locked);
  assert(result == SU_ERROR_NONE);
  assert(is_locked);
  
  // SUComponentInstanceSaveAs
  result = SUModelSaveToFile(model, "C:\\Users\\tthomas2\\SourceTree\\SLAPI-Test\\models\\SUComponentInstanceSetLocked.skp");
  assert(result == SU_ERROR_NONE);

  SUModelRelease(&model);

  SUTerminate();
  return 0;
}

The resulting file:

And the SKP file it reads in: SU-31417.skp (30.2 KB)

Thanks for the code,

I have tested the sample code with the input test file but when I open the generated output file in Sketchup Make 2016, the part is not locked. However if I test the same part to see whether there is or not a lot on the entities instance, it has a lock…

Attached output file.SU-31417_lock.skp (30.2 KB)

Am I using the file wrongly in Sketchup?

When I open this output file in SU2016, Zoom extents, then select the “UV Cube”, I see a properly locked Solid Group, not a ComponentInstance. (And being a group, it’s definition name is hidden in the EntityInfo inspector.)

My hidden select color is set to Red, so the boundingbox hilightes as Red.

So, why is the above screenshot showing a ComponentInstance ?

You lost me on this one … there isn’t a lock, but there is a lock?

Btw, are you opening the SKP, or are you importing it? If you import an SKP it will come in as a component. Your filename and screenshot indicate you have the “SU-31417_lock.skp” imported as a component.

Just to make sure, if you used my test script - it generated a file “SUComponentInstanceSetLocked.skp”. If you go to File > Open and then select that file in SketchUp you should get a group with a cube that is locked.

Can you try those exact steps?

1 Like

Hi,

I have managed to have correct behavior in Sketchup with the sample, I have realized that the model I have built with my code appeared to be a surface when I open in Sketchup not a solid model.

I guess something is wrong in the way I am building the component. I’ll open a new topic for this one to understand what I missed to build a Sketchup model from a tessellated representation.

Thanks a lot for your help.

Entity Info will only show “Solid Group” when you have the group selected. When you open the group/component and select the faces it will never say solid.

From the screenshot it looks like you have created a model with a cube - and opened that directly. It’s a bit unclear to what it is you are actually doing when you create and open/import these models. Maybe you can explain step by step what you do - or even better; a video?

Hi,

In order to open the file I just double click on it and let Sketchup 2016 open.

Then, I select the model on the scene and right click to display entity info.

I have created the following discussion which illustrate how I create the cube : Issue when creating a SKP model from tesselated representation: