The following source code sample demonstrates how to synchronize the basic material color of an object with the display color of the object. If the object does not have a material assigned to it, a new material is created and assigned to the object.
CRhinoCommand::result CCommandTest::RunCommand( const CRhinoCommandContext& context )
{
int num_modified = 0;
CRhinoObjectIterator it( CRhinoObjectIterator::normal_objects,
CRhinoObjectIterator::active_objects );
it.IncludeLights( FALSE );
CRhinoObject* obj = 0;
for( obj = it.First(); obj; obj = it.Next() )
{
// If the object gets its color from its layer, then we will
// let the layer handle its material color as well.
ON::object_color_source color_source = obj->Attributes().ColorSource();
if( color_source != ON::color_from_object )
continue;
int material_index = obj->Attributes().m_material_index;
if( material_index < 0 )
{
// If material_index < 0, then the object does not have a
// material assigned to it. So, we will create a new material
// that is based on Rhino's default material, and add it
// to the material table.
ON_Material material( RhinoApp().AppSettings().DefaultMaterial() );
material_index = context.m_doc.m_material_table.AddMaterial( material );
if( material_index >= 0 )
{
// Now that we have added the new material,
// assign it to the object.
CRhinoObjectAttributes new_attributes( obj->Attributes() );
new_attributes.m_material_index = material_index;
// Make sure to set the material source to "from object"
new_attributes.SetMaterialSource( ON::material_from_object );
obj->ModifyAttributes( new_attributes );
}
}
if( material_index < 0 )
continue;
const CRhinoMaterial& material = context.m_doc.m_material_table[material_index];
if( obj->Attributes().DrawColor() == material.Diffuse() )
continue;
// Modify the material's basic,or diffuse, color
ON_Material new_material( material );
new_material.SetDiffuse( obj->Attributes().DrawColor() );
context.m_doc.m_material_table.ModifyMaterial( new_material,
material.MaterialIndex(),
FALSE );
num_modified++;
}
if( num_modified > 0 )
context.m_doc.Regen();
return CRhinoCommand::success;
}
Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext) _
As RMA.Rhino.IRhinoCommand.result
Dim num_modified As Integer = 0
Dim it As New MRhinoObjectIterator( _
IRhinoObjectIterator.object_state.normal_objects, _
IRhinoObjectIterator.object_category.active_objects)
it.IncludeLights(False)
For Each obj As MRhinoObject In it
' If the object gets its color from its layer, then we will
' let the layer handle its material color as well.
Dim color_source As IOn.object_color_source = obj.Attributes().ColorSource()
If (color_source <> IOn.object_color_source.color_from_object) Then
Continue For
End If
Dim material_index As Integer = obj.Attributes().m_material_index
If (material_index < 0) Then
' If material_index < 0, then the object does not have a
' material assigned to it. So, we will create a new material
' that is based on Rhino's default material, and add it
' to the material table.
Dim mat As New OnMaterial(RhUtil.RhinoApp().AppSettings().DefaultMaterial())
material_index = context.m_doc.m_material_table.AddMaterial(mat)
If (material_index >= 0) Then
' Now that we have added the new material, assign it to the object.
Dim new_attributes As New MRhinoObjectAttributes(obj.Attributes())
new_attributes.m_material_index = material_index
' Make sure to set the material source to "from object"
new_attributes.SetMaterialSource(IOn.object_material_source.material_from_object)
obj.ModifyAttributes(new_attributes)
End If
End If
If (material_index < 0) Then Continue For
Dim material As IRhinoMaterial = context.m_doc.m_material_table(material_index)
If (obj.Attributes().DrawColor().Compare(material.m_diffuse) = 0) Then Continue For
' Modify the material's basic,or diffuse, color
Dim new_material As New OnMaterial(material)
new_material.m_diffuse = obj.Attributes().DrawColor()
context.m_doc.m_material_table.ModifyMaterial( new_material, _
material.m_material_index, _
False)
num_modified = num_modified + 1
Next
If (num_modified > 0) Then context.m_doc.Regen()
Return IRhinoCommand.result.success
End Function
public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
int num_modified = 0;
MRhinoObjectIterator it = new MRhinoObjectIterator(
IRhinoObjectIterator.object_state.normal_objects,
IRhinoObjectIterator.object_category.active_objects);
it.IncludeLights(false);
foreach (MRhinoObject obj in it)
{
// If the object gets its color from its layer, then we will
// let the layer handle its material color as well.
IOn.object_color_source color_source = obj.Attributes().ColorSource();
if(color_source != IOn.object_color_source.color_from_object)
continue;
int material_index = obj.Attributes().m_material_index;
if(material_index < 0)
{
// If material_index < 0, then the object does not have a
// material assigned to it. So, we will create a new material
// that is based on Rhino's default material, and add it
// to the material table.
OnMaterial mat = new OnMaterial(RhUtil.RhinoApp().AppSettings().DefaultMaterial());
material_index = context.m_doc.m_material_table.AddMaterial(mat);
if(material_index >= 0)
{
// Now that we have added the new material, assign it to the object.
MRhinoObjectAttributes new_attrib =
new MRhinoObjectAttributes(obj.Attributes());
new_attrib.m_material_index = material_index;
// Make sure to set the material source to "from object"
new_attrib.SetMaterialSource(IOn.object_material_source.material_from_object);
obj.ModifyAttributes(new_attrib);
}
}
if(material_index < 0)
continue;
IRhinoMaterial material = context.m_doc.m_material_table[material_index];
if(obj.Attributes().DrawColor().Compare(material.m_diffuse) == 0)
continue;
// Modify the material's basic,or diffuse, color
OnMaterial new_material = new OnMaterial(material);
new_material.m_diffuse = obj.Attributes().DrawColor();
context.m_doc.m_material_table.ModifyMaterial(new_material,
material.m_material_index,
false);
num_modified++;
}
if (num_modified > 0)
context.m_doc.Regen();
return IRhinoCommand.result.success;
}