Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:rhinocommonsamples:modifyobjectcolor [2015/09/14] (current)
Line 1: Line 1:
 +===== Sample: Modify an Object'​s Color =====
 +
 +===== C# =====
 +<code c#>
 +public class ModifyObjectColorCommand : Command
 +{
 +  public override string EnglishName { get { return "​csModifyObjectColor";​ } }
 +
 +  protected override Result RunCommand(RhinoDoc doc, RunMode mode)
 +  {
 +    ObjRef obj_ref;
 +    var rc = RhinoGet.GetOneObject(
 +      "​Select object",​ false, ObjectType.AnyObject,​ out obj_ref);
 +    if (rc != Result.Success)
 +      return rc;
 +    var rhino_object = obj_ref.Object();​
 +    var color = rhino_object.Attributes.ObjectColor;​
 +    bool b = Rhino.UI.Dialogs.ShowColorDialog(ref color);
 +    if (!b) return Result.Cancel;​
 +
 +    rhino_object.Attributes.ObjectColor = color;
 +    rhino_object.Attributes.ColorSource = ObjectColorSource.ColorFromObject;​
 +    rhino_object.CommitChanges();​
 +
 +    // an object'​s color attributes can also be specified
 +    // when the object is added to Rhino
 +    var sphere = new Sphere(Point3d.Origin,​ 5.0);
 +    var attributes = new ObjectAttributes();​
 +    attributes.ObjectColor = Color.CadetBlue;​
 +    attributes.ColorSource = ObjectColorSource.ColorFromObject;​
 +    doc.Objects.AddSphere(sphere,​ attributes);​
 +
 +    doc.Views.Redraw();​
 +    return Result.Success;​
 +  }
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +Public Class ModifyObjectColorCommand
 +  Inherits Command
 +  Public Overrides ReadOnly Property EnglishName() As String
 +    Get
 +      Return "​vbModifyObjectColor"​
 +    End Get
 +  End Property
 +
 +  Protected Overrides Function RunCommand(doc As RhinoDoc, mode As RunMode) As Result
 +    Dim obj_ref As ObjRef = Nothing
 +    Dim rc = RhinoGet.GetOneObject("​Select object",​ False, ObjectType.AnyObject,​ obj_ref)
 +    If rc <> Result.Success Then
 +      Return rc
 +    End If
 +    Dim rhino_object = obj_ref.[Object]()
 +    Dim color__1 = rhino_object.Attributes.ObjectColor
 +    Dim b As Boolean = Rhino.UI.Dialogs.ShowColorDialog(color__1)
 +    If Not b Then
 +      Return Result.Cancel
 +    End If
 +
 +    rhino_object.Attributes.ObjectColor = color__1
 +    rhino_object.Attributes.ColorSource = ObjectColorSource.ColorFromObject
 +    rhino_object.CommitChanges()
 +
 +    ' an object'​s color attributes can also be specified
 +    ' when the object is added to Rhino
 +    Dim sphere = New Sphere(Point3d.Origin,​ 5.0)
 +    Dim attributes = New ObjectAttributes()
 +    attributes.ObjectColor = Color.CadetBlue
 +    attributes.ColorSource = ObjectColorSource.ColorFromObject
 +    doc.Objects.AddSphere(sphere,​ attributes)
 +
 +    doc.Views.Redraw()
 +    Return Result.Success
 +  End Function
 +End Class
 +d Namespace
 +</​code>​
 +===== Python =====
 +<code python>
 +from System.Drawing import *
 +from Rhino import *
 +from Rhino.DocObjects import *
 +from Rhino.Geometry import *
 +from Rhino.Input import *
 +from Rhino.Commands import *
 +from Rhino.UI.Dialogs import ShowColorDialog
 +from scriptcontext import doc
 +
 +def RunCommand():​
 +  rc, obj_ref = RhinoGet.GetOneObject("​Select object",​ False, ObjectType.AnyObject)
 +  if rc <> Result.Success:​
 +    return rc
 +  rhino_object = obj_ref.Object()
 +  color = rhino_object.Attributes.ObjectColor
 +  b, color = ShowColorDialog(color)
 +  if not b: return Result.Cancel
 +
 +  rhino_object.Attributes.ObjectColor = color
 +  rhino_object.Attributes.ColorSource = ObjectColorSource.ColorFromObject
 +  rhino_object.CommitChanges()
 +
 +  # an object'​s color attributes can also be specified
 +  # when the object is added to Rhino
 +  sphere = Sphere(Point3d.Origin,​ 5.0)
 +  attributes = ObjectAttributes()
 +  attributes.ObjectColor = Color.CadetBlue
 +  attributes.ColorSource = ObjectColorSource.ColorFromObject
 +  doc.Objects.AddSphere(sphere,​ attributes)
 +
 +  doc.Views.Redraw()
 +  return Result.Success
 +
 +if __name__ == "​__main__":​
 +  RunCommand()
 +</​code>​
 +
 +{{tag>​Developer rhinocommon}}
  
developer/rhinocommonsamples/modifyobjectcolor.txt ยท Last modified: 2015/09/14 (external edit)