Rhino Update Object Groups With c++

Hi;
What’s wrong with my code ?

void RhinoUpdateObjectGroups(CRhinoObject* obj)
{
	ON_ClassArray<ON_SimpleArray<int>> gmap;
	int attrib_group_count = obj->Attributes().GroupCount();
	if (attrib_group_count > 0)
	{
		CRhinoObjectAttributes attributes = obj->Attributes();
		ON_SimpleArray<int> group_indexes;
		const int group_count = attributes.GetGroupList(group_indexes);
		if (group_indexes.Count() > 0)
		{
			if (group_count > 0)
			{
				if (gmap.Count() == 0)
				{
					ON_SimpleArray<int> g;
					for (int i = 0; i < group_count; i++)
					{
						g.Append(-1);
						gmap.Append(g);
					}
				}
				for (int i = 0; i < attrib_group_count; i++)
				{
					int old_group_index = group_indexes[i];
					int new_group_index = gmap[old_group_index][0];
					if (new_group_index == -1)
					{
						new_group_index = obj->Document()->m_group_table.AddGroup();
						gmap[old_group_index][0] = new_group_index;
					}
					group_indexes[i] = new_group_index;
				}
				attributes.RemoveFromAllGroups();
				for (int i = 0; i < attrib_group_count; i++)
					attributes.AddToGroup(group_indexes[i]);
				RhinoApp().ActiveDoc()->ModifyObjectAttributes(obj, attributes);
			}
		}
	}

}

Hi @pythonuser,

What is the code supposed to do?

– Dale

For example,If I want to move objects in the group, if I select a group, the objects affter move will group with the old objects, but I do not need this, I need them divided into two groups.

    CRhinoGetObject go;
	go.SetCommandPrompt(L"select objects");
	go.EnableGroupSelect(true);
	go.GetObjects(1, 0);
	if (go.CommandResult() != CRhinoCommand::success)
		return go.CommandResult();
	for (int i = 0; i < go.ObjectCount(); i++)
	{
		ON_Xform xf = ON_Xform::TranslationTransformation(RhinoApp().ActiveView()->Viewport().ConstructionPlane().m_plane.zaxis);
		RhinoApp().ActiveDoc()->TransformObject(go.Object(i), xf,false,false,true);
	}
	context.m_doc.Redraw();

Hi @pythonuser,

You might want to just use the RhinoUpdateObjectGroups function. You can find a sample of it’s usage here:

cmdSampleCopyInPlace.cpp

– Dale

Hi @dale:
Thank you very much, this sample is very helpful.