Example for adding object to layer C++

Hi,

I would like to ask if there an example how to:

  1. Check if a layer with a certain name exists
  2. If not create layer
  3. Then add objects to that layer

Currently I am baking objects while adding them to and I would like to add those groups to a specific layer:

	for (auto& output : output_polyline_groups) {
		
		ON_SimpleArray<const CRhinoObject*> group_members(output.size());
		for (int i = 0; i < output.size(); i++) {

			ON_3dPointArray points;
			points.Reserve(output[i].size());
			for (size_t j = 0; j < output[i].size(); j++)
				points.Append(ON_3dPoint(output[i][j].hx(), output[i][j].hy(), output[i][j].hz()));

			ON_Polyline pline(points);
			CRhinoCurveObject* curve_object = context.m_doc.AddCurveObject(pline);
			group_members.Append(curve_object);
		}

		int group_index = context.m_doc.m_group_table.AddGroup(ON_Group(), group_members);
		if (group_index < 0) CRhinoCommand::failure;
	}

Maybe something like this?

static int RhinoFindOrCreateLayer(CRhinoDoc& doc, const wchar_t* layer_name)
{
  if (nullptr == layer_name || 0 == layer_name[0])
    return ON_UNSET_INT_INDEX;

  int layer_index = doc.m_layer_table.FindLayerFromFullPathName(layer_name, ON_UNSET_INT_INDEX);
  if (layer_index == ON_UNSET_INT_INDEX)
  {
    ON_Layer layer;
    layer.SetName(layer_name);
    layer_index = doc.m_layer_table.AddLayer(layer);
  }

  return layer_index;
}

CRhinoCommand::result CCommandTest::RunCommand(const CRhinoCommandContext& context)
{
  const wchar_t* layer_name = L"MyLayer";
  int layer_index = RhinoFindOrCreateLayer(context.m_doc, layer_name);
  if (layer_index == ON_UNSET_INT_INDEX)
    return CRhinoCommand::failure;

  ON_3dmObjectAttributes attributes;
  context.m_doc.GetDefaultObjectAttributes(attributes);
  attributes.m_layer_index = layer_index;

  ON_Circle circle(ON_Plane::World_xy, 5.0);
  ON_ArcCurve arc_curve(circle);
  context.m_doc.AddCurveObject(arc_curve, &attributes);

  context.m_doc.Redraw();

  return CRhinoCommand::success;

– Dale

1 Like

Works perfectly thank you.

Dear @Dale,

How can I hide the previously main layer (change its visibility) after creating a new layer?

I tried to do a) retrieve current layer index, then set a current layer to newly created layer, and then hide the previously active layer, but this does not change the layer visibility:

   int current_layer_index = doc.m_layer_table.CurrentLayerIndex();
	doc.m_layer_table.SetCurrentLayerIndex(layer_index,true);

	ON_Layer layer_current = doc.m_layer_table[current_layer_index];
	layer_current.SetVisible(false);
	layer_current.SetPersistentVisibility(false);
	doc.m_layer_table.ModifyLayer(layer_current, current_layer_index, true);

Full code:

inline int RhinoFindOrCreateLayer(CRhinoDoc& doc, const wchar_t* layer_name) {
	if (nullptr == layer_name || 0 == layer_name[0])
		return ON_UNSET_INT_INDEX;

	int layer_index = doc.m_layer_table.FindLayerFromFullPathName(layer_name, ON_UNSET_INT_INDEX);


	if (layer_index == ON_UNSET_INT_INDEX) {
		ON_Layer layer;
		layer.SetName(layer_name);
		layer.SetColor(ON_Color(255, 0, 0));
		layer.SetPlotColor(ON_Color(255, 0, 0));
		layer_index = doc.m_layer_table.AddLayer(layer);
	}

	int current_layer_index = doc.m_layer_table.CurrentLayerIndex();
	doc.m_layer_table.SetCurrentLayerIndex(layer_index,true);

	ON_Layer layer_current = doc.m_layer_table[current_layer_index];
	layer_current.SetVisible(false);
	layer_current.SetPersistentVisibility(false);
	doc.m_layer_table.ModifyLayer(layer_current, current_layer_index, true);

	return layer_index;
}

Hmm, this actually works.

Is any method to hide all layers except the current one?

No, sorry.

– Dale