McNeel Wiki
How To: Modify an Object's Color
edit · print · help · all topics
Main Pages

AccuRender

Bongo

Brazil r/s

Developer

Flamingo

Penguin

Rhino Blogs

Rhino

Rhino Labs

Search

Languages

Česky

Deutsch

English

Español

Français

Italiano

Polish

日本語

한국어

中文(繁體)

 
.
DeveloperC++, .NET
SummaryDiscuss how to modify an object's color with the Rhino SDK.

The color used to display an object is specified in one of four ways.

  1. ON::color_from_layer - the object's layer color, ON_Layer::Color(), determines the object's color. This is the default method used when adding new objects to Rhino
  2. ON::color_from_object - the value of an object's m_color attribute determines the object's color.
  3. ON::color_from_material - the diffuse color of the object's render material determines the object's color.
  4. ON::color_from_parent - if the object is part of an instance reference, the color is taken from the instance

The following code sample demonstrates how to override the default "color by layer" behavior and set an object to use "color by object."

C++

  CRhinoCommand::result CCommandTest::RunCommand(const CRhinoCommandContext& context)
  {
    CRhinoGetObject go;
    go.SetCommandPrompt( L"Select object" );
    go.GetObjects( 1, 1 );
    if( go.CommandResult() != CRhinoCommand::success )
      return go.CommandResult();


    const CRhinoObjRef& obj_ref = go.Object( 0 );
    const CRhinoObject* obj = obj_ref.Object();
    if( !obj )
      return CRhinoCommand::failure;


    ON_Color old_color = obj->Attributes().DrawColor();
    ON::object_color_source color_source = obj->Attributes().ColorSource();
    ON_Color new_color( old_color );


    if( !RhinoColorDialog( ::RhinoApp().MainWnd(), new_color) )
      return CRhinoCommand::cancel;


    if( new_color == old_color )
      return CRhinoCommand::nothing;


    CRhinoObjectAttributes att( obj->Attributes() );
    att.m_color = new_color;
    att.SetColorSource( ON::color_from_object );
    context.m_doc.ModifyObjectAttributes( obj_ref, att );
    context.m_doc.Redraw();
    return CRhinoCommand::success;
  }

VB.NET (Rhino 4)

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


    Dim go As New MRhinoGetObject()
    go.SetCommandPrompt("Select object")
    go.GetObjects(1, 1)
    If (go.CommandResult() <> IRhinoCommand.result.success) Then
      Return go.CommandResult()
    End If


    Dim objref As IRhinoObjRef = go.Object(0)
    Dim obj As IRhinoObject = objref.Object()
    If (obj Is Nothing) Then Return IRhinoCommand.result.failure


    Dim old_color As OnColor = obj.Attributes().DrawColor()
    Dim color_source As IOn.object_color_source = obj.Attributes().ColorSource()
    Dim new_color As New OnColor(CType(old_color, IOnColor))


    If (Not RhUtil.RhinoColorDialog(RhUtil.RhinoApp.MainWnd(), new_color)) Then
      Return IRhinoCommand.result.cancel
    End If


    If (new_color.Compare(old_color) = 0) Then Return IRhinoCommand.result.nothing


    Dim att As New MRhinoObjectAttributes(obj.Attributes())
    att.m_color = new_color
    att.SetColorSource(IOn.object_color_source.color_from_object)
    context.m_doc.ModifyObjectAttributes(objref, att)
    context.m_doc.Redraw()
    Return IRhinoCommand.result.success
  End Function

C# (Rhino 4)

  public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
  {
    MRhinoGetObject go = new MRhinoGetObject();
    go.SetCommandPrompt("Select object");
    go.GetObjects(1, 1);
    if(go.CommandResult() != IRhinoCommand.result.success)
      return go.CommandResult();


    IRhinoObjRef objref = go.Object(0);
    IRhinoObject obj = objref.Object();
    if(obj == null)
      return IRhinoCommand.result.failure;


    OnColor old_color = obj.Attributes().DrawColor();
    IOn.object_color_source color_source = obj.Attributes().ColorSource();
    OnColor new_color = new OnColor((IOnColor)old_color);


    if(!RhUtil.RhinoColorDialog(RhUtil.RhinoApp().MainWnd(), ref new_color))
      return IRhinoCommand.result.cancel;


    if( new_color.Compare(old_color) == 0 )
      return IRhinoCommand.result.nothing;


    MRhinoObjectAttributes att = new MRhinoObjectAttributes(obj.Attributes());
    att.m_color = new_color;
    att.SetColorSource(IOn.object_color_source.color_from_object);
    context.m_doc.ModifyObjectAttributes(objref, att);
    context.m_doc.Redraw();
    return IRhinoCommand.result.success;
  }

When adding new objects using the SDK, you can specify the attributes of the object when adding it to Rhino. Thus, if you want to override the default color behavior for new objects, just get a copy of the active document's default new object attributes, modify it in whatever way you want, and pass it (along with the geometry) to the appropriate object creation function.

C++

  CRhinoCommand::result CCommandTest::RunCommand(const CRhinoCommandContext& context)
  {
    ON_Circle circle;
    circle.Create( RhinoActiveCPlane(), 5.0 );
    ON_3dmObjectAttributes att;
    context.m_doc.GetDefaultObjectAttributes( att );


    att.m_color = RGB(255,191,191);
    att.SetColorSource( ON::color_from_object );
    context.m_doc.AddCurveObject( circle, &att );
    context.m_doc.Redraw();
    return CRhinoCommand::success;
  }

VB.NET (Rhino 4)

  Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext) _
    As IRhinoCommand.result
    Dim circle As New OnCircle()
    circle.Create(RhUtil.RhinoActiveCPlane(), 5.0)
    Dim att As New On3dmObjectAttributes()
    context.m_doc.GetDefaultObjectAttributes(att)


    att.m_color = New OnColor(System.Drawing.Color.CadetBlue)
    att.SetColorSource(IOn.object_color_source.color_from_object)
    context.m_doc.AddCurveObject(circle, att)
    context.m_doc.Redraw()
    Return IRhinoCommand.result.success
  End Function

C# (Rhino 4)

  public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
  {
    OnCircle circle = new OnCircle();
    circle.Create(RhUtil.RhinoActiveCPlane(), 5.0);
    On3dmObjectAttributes att = new On3dmObjectAttributes();
    context.m_doc.GetDefaultObjectAttributes(ref att);


    System.Drawing.Color cb = System.Drawing.Color.CadetBlue;
    att.m_color = new OnColor( ref cb);
    att.SetColorSource(IOn.object_color_source.color_from_object);
    context.m_doc.AddCurveObject(circle, att);
    context.m_doc.Redraw();
    return IRhinoCommand.result.success;
  }
rename · changes · history · subscriptions · lost and found · references · file upload