ON_Object::ObjectType() returns 0

I am trying to iterate through the model geometry list. But ON_Object::ObjectType() always returns zero…

ONX_Model model;
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* geo = ON_ModelGeometryComponent::Cast(model_component);
		if (nullptr != geo)
		{
			switch(geo->ObjectType())
			
			default:
				break;
			case ON::object_type::brep_object:
			//todo
			      break;

		}
		
}

Hi @rajeev,

ON::object_type is an old-style enum (declared as enum not enum class).

So instead of this:

case ON::object_type::brep_object:

You will want to do this:

case ON::brep_object:

Does this help?

– Dale

Thanks… that helped…

Using OpenNurbs v5, it was easy to find the layer, material and groups associated to the geometry objects, once the reference of the objects were available using ONX_Model_objects.

In the new scenario, I tried to cast the following without success…
//get layer index
const ON_ModelComponentReference model_ref_layer = model.LayerFromId(model_component->Id());
const ON_Layer* layer = ON_Layer::Cast(model_ref_layer.ModelComponent());
//get group index
const ON_ModelComponentReference model_ref_group = model.ComponentFromId(ON_ModelComponent::Type::Group, model_component->Id());
const ON_Group* group = ON_Group::Cast(model_ref_group.ModelComponent());
/get material index
const ON_ModelComponentReference model_ref_material = model.RenderMaterialFromId(model_component->Id());
const ON_Material* mat = ON_Material::Cast(model_ref_material.ModelComponent());

What is the correct way of finding the layer, material and groups, when we have the geometric objects.

Hi @rajeev,

In order to lookup referenced components, such as Layer, Material, Groups, etc., you first must obtain the model geometry attributes, or ON_3dmObjectAttributes object:

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)
    {
      // TODO
    }

Now that you have the attributes, you can use the helper functions on ONX_Model to access these components.

Here are a couple of helper functions you might find useful:

/*
Description:
  Returns a pointer to an ON_Layer object, given an object's attributes.
*/
static const ON_Layer* ON_LayerFromAttributes(
  const ONX_Model& model, 
  const ON_3dmObjectAttributes& attributes
)
{
  const ON_Layer* layer = nullptr;
  const ON_ModelComponentReference& model_component_ref = model.LayerFromAttributes(attributes);
  if (!model_component_ref.IsEmpty())
    layer = ON_Layer::Cast(model_component_ref.ModelComponent());
  return layer;
}

/*
Description:
  Returns a pointer to an ON_Material object, given an object's attributes.
*/
static const ON_Material* ON_MaterialFromAttributes(
  const ONX_Model& model, 
  const ON_3dmObjectAttributes& attributes
)
{
  const ON_Material* material = nullptr;
  const ON_ModelComponentReference& model_component_ref = model.RenderMaterialFromAttributes(attributes);
  if (!model_component_ref.IsEmpty())
    material = ON_Material::Cast(model_component_ref.ModelComponent());
  return material;
}

/*
Description:
  Returns pointers to ON_Group objects, given an object's attributes.
*/
static int ON_GroupsFromAttributes(const ONX_Model& model, const ON_3dmObjectAttributes& attributes, ON_SimpleArray<const ON_Group*>& groups)
{
  const int group_count = groups.Count();
  ON_SimpleArray<int> group_indices;
  if (attributes.GetGroupList(group_indices) > 0)
  {
    for (int i = 0; i < group_indices.Count(); i++)
    {
      ON_ModelComponentReference model_component_ref = model.ComponentFromIndex(ON_ModelComponent::Type::Group, i);
      if (!model_component_ref.IsEmpty())
      {
        const ON_Group* group = ON_Group::Cast(model_component_ref.ModelComponent());
        if (nullptr != group)
          groups.Append(group);
      }
    }
  }
  return groups.Count() - group_count;
}

Hope this helps.

– Dale

Thank you very much @dale

Wow! … I definitely went on a tangent. As soon as I get some time I will try to create a chart to illustrate the various links…

For those watching at home:

– Dale

1 Like