Change Light Object Color
Last changed: stevebaer-204.177.179.132

.
DeveloperC++, .NET
SummaryHow to modify the diffuse color of an exising CRhinoLight object.

The process for modifying a light object is slightly different than the process for modifying other geometric objects, such as points, curves, and surfaces. This is because light objects are stored in a different location in the Rhino document.

More Information

Light objects are stored in a CRhinoLightTable object that is held by the active document object. Thus, instead of using one of the ModifyObject() members found on CRhinoDoc, you need to use the ModifyLight() member found on CRhinoLightTable in order to modify an exising light object.

For more details on CRhinoLight and CRhinoLightTable, see rhinoSdkLight.h included with the SDK.

Example

The following example demonstrates how to modify the diffuse color of a light object.

C++

  CRhinoCommand::result CCommandTest::RunCommand( const CRhinoCommandContext& context )
  {
    // Pick an existing light object
    CRhinoGetObject go;
    go.SetCommandPrompt( L"Select light to change color" );
    go.SetGeometryFilter( CRhinoGetObject::light_object );
    go.GetObjects( 1, 1 );
    if( go.CommandResult() != CRhinoCommand::success )
      return go.CommandResult();


    // The the light object
    CRhinoObjRef& ref = go.Object(0);
    const CRhinoLight* light_obj = CRhinoLight::Cast( ref.Object() );
    if( !light_obj )
      return CRhinoCommand::failure;


    // Prompt the user to pick a new color
    ON_Color color = light_obj->Light().Diffuse();
    if( !RhinoColorDialog(RhinoApp().MainWnd(), color) )
      CRhinoCommand::cancel;


    // Copy the light object's underlying ON_Light
    ON_Light light( light_obj->Light() );
    // Modify the diffuse color
    light.SetDiffuse( color );


    // Modify the light
    CRhinoLightTable& light_table = context.m_doc.m_light_table;
    light_table.ModifyLight( light, light_obj->LightIndex() );


    return CRhinoCommand::success;
  }

VB.NET (Rhino 4)

  Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext)_
    As RMA.Rhino.IRhinoCommand.result


    ' Pick an existing light object
    Dim go As New MRhinoGetObject()
    go.SetCommandPrompt("Select light to change color")
    go.SetGeometryFilter(IRhinoGetObject.GEOMETRY_TYPE_FILTER.light_object)
    go.GetObjects(1, 1)
    If (go.CommandResult() <> IRhinoCommand.result.success) Then Return go.CommandResult()


    ' The the light object
    Dim objref As MRhinoObjRef = go.Object(0)
    Dim light_obj As IRhinoLight = MRhinoLight.ConstCast(objref.Object())
    If (light_obj Is Nothing) Then Return IRhinoCommand.result.failure


    ' Prompt the user to pick a new color
    Dim color As OnColor = light_obj.Light().Diffuse()
    If (Not RhUtil.RhinoColorDialog(RhUtil.RhinoApp.MainWnd(), color)) Then
      Return IRhinoCommand.result.cancel
    End If


    ' Copy the light object's underlying ON_Light
    Dim light As New OnLight(light_obj.Light())
    ' Modify the diffuse color
    light.SetDiffuse(color)


    ' Modify the light
    Dim light_table As MRhinoLightTable = context.m_doc.m_light_table
    light_table.ModifyLight(light, light_obj.LightIndex())


    Return IRhinoCommand.result.success
  End Function

C# (Rhino 4)

  public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
  {
    // Pick an existing light object
    MRhinoGetObject go = new MRhinoGetObject();
    go.SetCommandPrompt("Select light to change color");
    go.SetGeometryFilter(IRhinoGetObject.GEOMETRY_TYPE_FILTER.light_object);
    go.GetObjects(1, 1);
    if( go.CommandResult() != IRhinoCommand.result.success )
      return go.CommandResult();


    // The the light object
    MRhinoObjRef objref = go.Object(0);
    IRhinoLight light_obj = MRhinoLight.ConstCast(objref.Object());
    if( light_obj == null )
      return IRhinoCommand.result.failure;


    // Prompt the user to pick a new color
    OnColor color = light_obj.Light().Diffuse();
    if(!RhUtil.RhinoColorDialog(RhUtil.RhinoApp().MainWnd(), ref color))
      return IRhinoCommand.result.cancel;


    // Copy the light object's underlying ON_Light
    OnLight light = new OnLight(light_obj.Light());
    // Modify the diffuse color
    light.SetDiffuse(color);


    // Modify the light
    MRhinoLightTable light_table = context.m_doc.m_light_table;
    light_table.ModifyLight(light, light_obj.LightIndex());


    return IRhinoCommand.result.success;
  }