Site Tools


Differences

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

Link to this comparison view

developer:rhinocommonsamples:objectdisplaymode [2015/09/14] (current)
Line 1: Line 1:
 +===== Sample: Set object display mode =====
 +
 +===== C# =====
 +<code c#>
 +public static Rhino.Commands.Result ObjectDisplayMode(Rhino.RhinoDoc doc)
 +{
 +  Rhino.Commands.Result rc = Rhino.Commands.Result.Cancel;​
 +  ObjectType filter = ObjectType.Mesh | ObjectType.Brep;​
 +  ObjRef objref;
 +  rc = Rhino.Input.RhinoGet.GetOneObject("​Select mesh or surface",​ true, filter, out objref);
 +  if (rc != Rhino.Commands.Result.Success)
 +    return rc;
 +  Guid viewportId = doc.Views.ActiveView.ActiveViewportID;​
 +
 +  ObjectAttributes attr = objref.Object().Attributes;​
 +  if (attr.HasDisplayModeOverride(viewportId))
 +  {
 +    RhinoApp.WriteLine("​Removing display mode override from object"​);​
 +    attr.RemoveDisplayModeOverride(viewportId);​
 +  }
 +  else
 +  {
 +    Rhino.Display.DisplayModeDescription[] modes = Rhino.Display.DisplayModeDescription.GetDisplayModes();​
 +    Rhino.Display.DisplayModeDescription mode = null;
 +    if (modes.Length == 1)
 +      mode = modes[0];
 +    else
 +    {
 +      Rhino.Input.Custom.GetOption go = new Rhino.Input.Custom.GetOption();​
 +      go.SetCommandPrompt("​Select display mode"​);​
 +      string[] str_modes = new string[modes.Length];​
 +      for (int i = 0; i < modes.Length;​ i++)
 +        str_modes[i] = modes[i].EnglishName.Replace("​ ", ""​).Replace("​-",​ ""​);​
 +      go.AddOptionList("​DisplayMode",​ str_modes, 0);
 +      if (go.Get() == Rhino.Input.GetResult.Option)
 +        mode = modes[go.Option().CurrentListOptionIndex];​
 +    }
 +    if (mode == null)
 +      return Rhino.Commands.Result.Cancel;​
 +    attr.SetDisplayModeOverride(mode,​ viewportId);​
 +  }
 +  doc.Objects.ModifyAttributes(objref,​ attr, false);
 +  doc.Views.Redraw();​
 +  return Rhino.Commands.Result.Success;​
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +Public Shared Function ObjectDisplayMode(ByVal doc As Rhino.RhinoDoc) As Rhino.Commands.Result
 +  Dim rc As Rhino.Commands.Result = Rhino.Commands.Result.Cancel
 +  Dim filter As ObjectType = ObjectType.Mesh Or ObjectType.Brep
 +  Dim objref As ObjRef = Nothing
 +  rc = Rhino.Input.RhinoGet.GetOneObject("​Select mesh or surface",​ True, filter, objref)
 +  If rc <> Rhino.Commands.Result.Success Then
 +    Return rc
 +  End If
 +  Dim viewportId As Guid = doc.Views.ActiveView.ActiveViewportID
 +
 +  Dim attr As ObjectAttributes = objref.[Object]().Attributes
 +  If attr.HasDisplayModeOverride(viewportId) Then
 +    RhinoApp.WriteLine("​Removing display mode override from object"​)
 +    attr.RemoveDisplayModeOverride(viewportId)
 +  Else
 +    Dim modes As Rhino.Display.DisplayModeDescription() = Rhino.Display.DisplayModeDescription.GetDisplayModes()
 +    Dim mode As Rhino.Display.DisplayModeDescription = Nothing
 +    If modes.Length = 1 Then
 +      mode = modes(0)
 +    Else
 +      Dim go As New Rhino.Input.Custom.GetOption()
 +      go.SetCommandPrompt("​Select display mode")
 +      Dim str_modes As String() = New String(modes.Length - 1) {}
 +      For i As Integer = 0 To modes.Length - 1
 +        str_modes(i) = modes(i).EnglishName.Replace("​ ", ""​).Replace("​-",​ ""​)
 +      Next
 +      go.AddOptionList("​DisplayMode",​ str_modes, 0)
 +      If go.[Get]() = Rhino.Input.GetResult.[Option] Then
 +        mode = modes(go.[Option]().CurrentListOptionIndex)
 +      End If
 +    End If
 +    If mode Is Nothing Then
 +      Return Rhino.Commands.Result.Cancel
 +    End If
 +    attr.SetDisplayModeOverride(mode,​ viewportId)
 +  End If
 +  doc.Objects.ModifyAttributes(objref,​ attr, False)
 +  doc.Views.Redraw()
 +  Return Rhino.Commands.Result.Success
 +End Function
 +</​code>​
 +===== Python =====
 +<code python>
 +import Rhino
 +import scriptcontext
 +
 +def ObjectDisplayMode():​
 +    filter = Rhino.DocObjects.ObjectType.Brep | Rhino.DocObjects.ObjectType.Mesh
 +    rc, objref = Rhino.Input.RhinoGet.GetOneObject("​Select mesh or surface",​ True, filter)
 +    if rc != Rhino.Commands.Result.Success:​ return rc;
 +    viewportId = scriptcontext.doc.Views.ActiveView.ActiveViewportID
 +
 +    attr = objref.Object().Attributes
 +    if attr.HasDisplayModeOverride(viewportId):​
 +        print "​Removing display mode override from object"​
 +        attr.RemoveDisplayModeOverride(viewportId)
 +    else:
 +        modes = Rhino.Display.DisplayModeDescription.GetDisplayModes()
 +        mode = None
 +        if len(modes) == 1:
 +            mode = modes[0]
 +        else:
 +            go = Rhino.Input.Custom.GetOption()
 +            go.SetCommandPrompt("​Select display mode")
 +            str_modes = []
 +            for m in modes:
 +                s = m.EnglishName.replace("​ ",""​).replace("​-",""​)
 +                str_modes.append(s)
 +            go.AddOptionList("​DisplayMode",​ str_modes, 0)
 +            if go.Get()==Rhino.Input.GetResult.Option:​
 +                mode = modes[go.Option().CurrentListOptionIndex]
 +        if not mode: return Rhino.Commands.Result.Cancel
 +        attr.SetDisplayModeOverride(mode,​ viewportId)
 +    scriptcontext.doc.Objects.ModifyAttributes(objref,​ attr, False)
 +    scriptcontext.doc.Views.Redraw()
 +
 +
 +if __name__=="​__main__":​
 +    ObjectDisplayMode()
 +</​code>​
 +
 +{{tag>​Developer rhinocommon}}
  
developer/rhinocommonsamples/objectdisplaymode.txt ยท Last modified: 2015/09/14 (external edit)