CurrentLayerIndex() returns some garbage index for layer


(Sandip Ichake) #1

Hello Dale,

I am referring https://developer.rhino3d.com/guides/opennurbs/getting-object-attributes/
to fetch the layer information, but when I am using onxModel->m_settings.CurrentLayerIndex() API to get the current layer index it returns some garbage value.

Note: I have two layers added in the model, “Default” & “Layer01”. Layer01 is my current layer

Please advise.

Thanks & Regards,
Sandip Ichake


(Dale Fugier) #2

Hi @sandip.ichake,

Can you provide a sample 3dm file and source code, that I can run here, that isn’t working for you?

Thanks,

– Dale


(Sandip Ichake) #3

Hello Dale,

Please find the below code snippet, which I’m using to read the layer objects. Also I have attached, sample model created.

Line_L_Layer01_M_Plastic_L_Dots.3dm (30.4 KB)

 ONX_ModelComponentIterator it(m_onxModel, ON_ModelComponent::Type::ModelGeometry);
const ON_ModelComponent* model_component = nullptr;
int iLayer = 0;
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* layerP = nullptr;
            const ON_ModelComponentReference& model_component_ref = m_onxModel->LayerFromAttributes(*attributes);
            if (!model_component_ref.IsEmpty())
            {
                layerP = ON_Layer::Cast(model_component_ref.ModelComponent());
				.
				.
				.
				.
				int iCurrentLayer = m_onxModel->m_settings.CurrentLayerIndex();
			}
		}
	}
}

Is there any settings/prerequisite that needs to set before querying the CurrentLayerIndex() method?

I have also observed that, API’s with V5 names are showing similar garbage values, please find the below image for the same:


(Dale Fugier) #4

Hi @sandip.ichake,

Does this help?

static int ReadCurrentLayer(const wchar_t* filename)
{
  int current_layer_index = -1;

  if (nullptr == filename || 0 == filename[0])
    return current_layer_index;

  ONX_Model model;
  bool rc = model.Read(filename);
  if (!rc)
    return current_layer_index;

  ON_UUID current_layer_id = model.m_settings.CurrentLayerId();
  ON_ModelComponentReference model_component_ref = model.ComponentFromId(ON_ModelComponent::Type::Layer, current_layer_id);
  if (!model_component_ref.IsEmpty())
  {
    const ON_Layer* layer = ON_Layer::Cast(model_component_ref.ModelComponent());
    if (nullptr != layer)
      current_layer_index = layer->Index();
  }

  return current_layer_index;
}

– Dale


(Sandip Ichake) #5

Thank you Dale! This is helpful.

I have few questions:

  1. Is there any way to identify default layer? or How to know ON_Layer is default or not?
  2. Is default layer index(0) fixed?
  3. Is there any different mechanism to read default layer? or the code which I have mentioned above is sufficient to read default layer.

Thanks & Regards,
Sandip


(Dale Fugier) #6

Hi @sandip.ichake,

The code I posted above demonstrates how to obtain the current, or default, layer from a 3dm file.

– Dale