Why don't the materials exist when I add a material in a table ?(C++)


#1

I add a material in a table and has checked the index which indeed exists.
My code:

ON_Material Mat[5];
Mat[0].m_material_name = L"GtBT";
Mat[0].m_diffuse.SetRGB(0,0,255);
Mat[0].SetTransparency(0.5);
int mat_index = RhinoApp().ActiveDoc()->m_material_table.AddMaterial(Mat[0]);
if ( mat_index < 0 )
	return false;

Why can’t I find it in the view ?


(Dale Fugier) #2

Your new material does not appear in the Materials panel because it is not in use.

Try this:

CRhinoCommand::result CCommandTest::RunCommand(const CRhinoCommandContext& context)
{
  CRhinoCommand::result rc = CRhinoCommand::failure;

  ON_3dPoint center(0.0, 0.0, 0.0);
  ON_Sphere sphere(center, 5.0);
  ON_RevSurface rev_srf;
  if (sphere.RevSurfaceForm(true, &rev_srf))
  {
    ON_Material material(RhinoApp().AppSettings().DefaultMaterial());
    material.m_material_name = L"GtBT";
    material.m_diffuse.SetRGB(0, 0, 255);
    material.SetTransparency(0.5);
    int material_index = context.m_doc.m_material_table.AddMaterial(material);

    ON_3dmObjectAttributes atts;
    context.m_doc.GetDefaultObjectAttributes(atts);
    atts.SetMaterialSource(ON::material_from_object);
    atts.m_material_index = material_index;

    context.m_doc.AddSurfaceObject(rev_srf, &atts);
    context.m_doc.Redraw();
    rc = CRhinoCommand::success;
  }

  return rc;
}

#3

thank you for help. :smile:
I got it.