Object layers returned null

Hi,
Here is a .3dm file created by rhino3dm.
colored_cubes.3dm (15.6 KB)

Now I want to read the file using OpenNurbs, but can’t get the layer of any object - all of the layers are null.
Here is part of my code to get the geometry’s layer.

ONX_ModelComponentIterator it(onxModel, ON_ModelComponent::Type::ModelGeometry);
for (ON_ModelComponentReference mcr = it.FirstComponentReference(); false == mcr.IsEmpty(); mcr = it.NextComponentReference())
{
	const ON_ModelComponent* modelComponent = mcr.ModelComponent();
	if (modelComponent)
	{
		const ON_ModelGeometryComponent* mgc = dynamic_cast<const ON_ModelGeometryComponent*>(modelComponent);
		const ON_3dmObjectAttributes* model_geometry_attributes = mgc->Attributes(nullptr);
		const ON_ModelComponentReference& model_component_ref = onxModel.LayerFromAttributes(*attributes);
		const ON_Layer* layer = ON_Layer::Cast(model_component_ref.ModelComponent());
	}
}

Hi @sw11,

Try this:

static const ON_Layer* ModelGeometryLayer(
  const ONX_Model& model,
  const ON_3dmObjectAttributes& attributes
)
{
  ON_ModelComponentReference rc = model.LayerFromAttributes(attributes);
  if (!rc.IsEmpty())
    return ON_Layer::Cast(rc.ModelComponent());
  return nullptr;
}

//////////

  ONX_ModelComponentIterator it(model, ON_ModelComponent::Type::ModelGeometry);
  const ON_ModelComponent* model_component = nullptr;
  for (model_component = it.FirstComponent(); nullptr != model_component; model_component = it.NextComponent())
  {
    const ON_ModelGeometryComponent* model_geometry = ON_ModelGeometryComponent::Cast(model_component);
    if (nullptr != model_geometry)
    {
      const ON_3dmObjectAttributes* attributes = model_geometry->Attributes(nullptr);
      if (nullptr != attributes)
      {
        const ON_Layer* layer = ModelGeometryLayer(model, *attributes);
        if (nullptr != layer)
        {
          ON_wString layer_name = layer->Name();
          // TODO...
        }
      }
    }
  }

– Dale

Hi, @dale
I have tried your code, but the layer still returned null.

This works for me.

example_read.cpp (1.5 KB)

– Dale

Hi @dale,
I can get the correct layer when ON::Begin() is added. Thank you!
Another question, the color of each object seems different from that of default layer. How can I obtain the color of each object?

Hi @sw11,

See below:

const ON_ModelGeometryComponent* model_geometry = ON_ModelGeometryComponent::Cast(model_component);
if (nullptr != model_geometry)
{
  const ON_3dmObjectAttributes* attributes = model_geometry->Attributes(nullptr);
  if (nullptr != attributes)
  {
    ON_Color model_geometry_color = ON_Color::UnsetColor;
    if (attributes->ColorSource() == ON::color_from_object)
      model_geometry_color = attributes->m_color;
    else if (attributes->ColorSource() == ON::color_from_layer)
    {
      const ON_Layer* layer = ModelGeometryLayer(model, *attributes);
      if (nullptr != layer)
        model_geometry_color = layer->Color();
    }
  }
}

– Dale