sw11
(sw)
August 27, 2024, 7:53am
1
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());
}
}
dale
(Dale Fugier)
August 27, 2024, 8:30pm
2
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
sw11
(sw)
August 28, 2024, 2:22am
3
Hi, @dale
I have tried your code, but the layer still returned null.
dale
(Dale Fugier)
August 28, 2024, 6:24pm
4
This works for me.
example_read.cpp (1.5 KB)
– Dale
sw11
(sw)
August 29, 2024, 3:11am
5
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?
dale
(Dale Fugier)
August 29, 2024, 2:51pm
6
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