Site Tools


Differences

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

Link to this comparison view

developer:rhinocommonsamples:conduitarrowheads [2015/09/14] (current)
Line 1: Line 1:
 +====== Sample: Drawing Arrowheads in a Display Conduit ======
 +
 +===== C# =====
 +<code c#>
 +class DrawArrowHeadsConduit : Rhino.Display.DisplayConduit
 +{
 +  private readonly Line _line;
 +  private readonly int _screenSize;​
 +  private readonly double _worldSize;
 +
 +  public DrawArrowHeadsConduit(Line line, int screenSize, double worldSize)
 +  {
 +    _line = line;
 +    _screenSize = screenSize;
 +    _worldSize = worldSize;
 +  }
 +
 +  protected override void DrawForeground(Rhino.Display.DrawEventArgs e)
 +  {
 +    e.Display.DrawArrow(_line,​ System.Drawing.Color.Black,​ _screenSize,​ _worldSize);​
 +  }
 +}
 +
 +[System.Runtime.InteropServices.Guid("​A7236E94-85BD-4D63-9950-19C268E63661"​)]
 +public class DrawArrowheadsCommand : Command
 +{
 +  public override string EnglishName { get { return "​csDrawArrowHeads";​ } }
 +
 +  protected override Result RunCommand(RhinoDoc doc, RunMode mode)
 +  {
 +    // get arrow head size
 +    var go = new GetOption();​
 +    go.SetCommandPrompt("​ArrowHead length in screen size (pixles) or world size (percentage of arrow lenght)?"​);​
 +    go.AddOption("​screen"​);​
 +    go.AddOption("​world"​);​
 +    go.Get();
 +    if (go.CommandResult() != Result.Success)
 +      return go.CommandResult();​
 +
 +    int screenSize = 0;
 +    double worldSize = 0.0;
 +    if (go.Option().EnglishName == "​screen"​)
 +    {
 +      var gi = new GetInteger();​
 +      gi.SetLowerLimit(0,​true);​
 +      gi.SetCommandPrompt("​Length of arrow head in pixels"​);​
 +      gi.Get();
 +      if (gi.CommandResult() != Result.Success)
 +        return gi.CommandResult();​
 +      screenSize = gi.Number();​
 +    }
 +    else
 +    {
 +      var gi = new GetInteger();​
 +      gi.SetLowerLimit(0,​ true);
 +      gi.SetUpperLimit(100,​ false);
 +      gi.SetCommandPrompt("​Lenght of arrow head in percentage of total arrow lenght"​);​
 +      gi.Get();
 +      if (gi.CommandResult() != Result.Success)
 +        return gi.CommandResult();​
 +      worldSize = gi.Number()/​100.0;​
 +    }
 +
 +
 +    // get arrow start and end points
 +    var gp = new GetPoint();
 +    gp.SetCommandPrompt("​Start of line"​);​
 +    gp.Get();
 +    if (gp.CommandResult() != Result.Success)
 +      return gp.CommandResult();​
 +    var startPoint = gp.Point();
 +
 +    gp.SetCommandPrompt("​End of line"​);​
 +    gp.SetBasePoint(startPoint,​ false);
 +    gp.DrawLineFromPoint(startPoint,​ true);
 +    gp.Get();
 +    if (gp.CommandResult() != Result.Success)
 +      return gp.CommandResult();​
 +    var endPoint = gp.Point();
 +
 +    var v = endPoint - startPoint;
 +    if (v.IsTiny(Rhino.RhinoMath.ZeroTolerance))
 +      return Result.Nothing;​
 +
 +    var line = new Line(startPoint,​ endPoint);
 +
 +    var conduit = new DrawArrowHeadsConduit(line,​ screenSize, worldSize);
 +    // toggle conduit on/off
 +    conduit.Enabled = !conduit.Enabled;​
 +    RhinoApp.WriteLine("​draw arrowheads conduit enabled = {0}", conduit.Enabled);​
 +
 +    doc.Views.Redraw();​
 +    return Result.Success;​
 +  }
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +Class DrawArrowHeadsConduit
 +  Inherits Rhino.Display.DisplayConduit
 +  Private _line As Line
 +  Private _screenSize As Integer
 +  Private _worldSize As Double
 +
 +  Public Sub New(line As Line, screenSize As Integer, worldSize As Double)
 +    _line = line
 +    _screenSize = screenSize
 +    _worldSize = worldSize
 +  End Sub
 +
 +  Protected Overrides Sub DrawForeground(e As Rhino.Display.DrawEventArgs)
 +    e.Display.DrawArrow(_line,​ System.Drawing.Color.Black,​ _screenSize,​ _worldSize)
 +  End Sub
 +End Class
 +
 +<​System.Runtime.InteropServices.Guid("​65FE68EE-B809-4F47-B88A-3B3A944FF44C"​)>​ _
 +Public Class DrawArrowheadsCommand
 +  Inherits Command
 +  Public Overrides ReadOnly Property EnglishName() As String
 +    Get
 +      Return "​vbDrawArrowHeads"​
 +    End Get
 +  End Property
 +
 +  Protected Overrides Function RunCommand(doc As RhinoDoc, mode As RunMode) As Result
 +    ' get arrow head size
 +    Dim go = New Rhino.Input.Custom.GetOption()
 +    go.SetCommandPrompt("​ArrowHead length in screen size (pixles) or world size (percentage of arrow lenght)?"​)
 +    go.AddOption("​screen"​)
 +    go.AddOption("​world"​)
 +    go.[Get]()
 +    If go.CommandResult() <> Result.Success Then
 +      Return go.CommandResult()
 +    End If
 +
 +    Dim screenSize As Integer = 0
 +    Dim worldSize As Double = 0.0
 +    If go.[Option]().EnglishName = "​screen"​ Then
 +      Dim gi = New Rhino.Input.Custom.GetInteger()
 +      gi.SetLowerLimit(0,​ True)
 +      gi.SetCommandPrompt("​Length of arrow head in pixels"​)
 +      gi.[Get]()
 +      If gi.CommandResult() <> Result.Success Then
 +        Return gi.CommandResult()
 +      End If
 +      screenSize = gi.Number()
 +    Else
 +      Dim gi = New Rhino.Input.Custom.GetInteger()
 +      gi.SetLowerLimit(0,​ True)
 +      gi.SetUpperLimit(100,​ False)
 +      gi.SetCommandPrompt("​Lenght of arrow head in percentage of total arrow lenght"​)
 +      gi.[Get]()
 +      If gi.CommandResult() <> Result.Success Then
 +        Return gi.CommandResult()
 +      End If
 +      worldSize = gi.Number() / 100.0
 +    End If
 +
 +
 +    ' get arrow start and end points
 +    Dim gp = New Rhino.Input.Custom.GetPoint()
 +    gp.SetCommandPrompt("​Start of line")
 +    gp.[Get]()
 +    If gp.CommandResult() <> Result.Success Then
 +      Return gp.CommandResult()
 +    End If
 +    Dim startPoint = gp.Point()
 +
 +    gp.SetCommandPrompt("​End of line")
 +    gp.SetBasePoint(startPoint,​ False)
 +    gp.DrawLineFromPoint(startPoint,​ True)
 +    gp.[Get]()
 +    If gp.CommandResult() <> Result.Success Then
 +      Return gp.CommandResult()
 +    End If
 +    Dim endPoint = gp.Point()
 +
 +    Dim v = endPoint - startPoint
 +    If v.IsTiny(Rhino.RhinoMath.ZeroTolerance) Then
 +      Return Result.[Nothing]
 +    End If
 +
 +    Dim line = New Line(startPoint,​ endPoint)
 +
 +    Dim conduit = New DrawArrowHeadsConduit(line,​ screenSize, worldSize)
 +    ' toggle conduit on/off
 +    conduit.Enabled = Not conduit.Enabled
 +    RhinoApp.WriteLine("​draw arrowheads conduit enabled = {0}", conduit.Enabled)
 +
 +    doc.Views.Redraw()
 +    Return Result.Success
 +  End Function
 +End Class
 +d Namespace
 +</​code>​
 +===== Python =====
 +<code python>
 +import Rhino
 +import System.Drawing
 +import scriptcontext
 +import rhinoscriptsyntax as rs
 +
 +class DrawArrowHeadsConduit(Rhino.Display.DisplayConduit):​
 +  def __init__(self,​ line, screenSize, worldSize):
 +    self.line = line
 +    self.screenSize = screenSize
 +    self.worldSize = worldSize
 +
 +  def DrawForeground(self,​ e):
 +    e.Display.DrawArrow(self.line,​ System.Drawing.Color.Black,​ self.screenSize,​ self.worldSize)
 +
 +def RunCommand():​
 +  # get arrow head size
 +  go = Rhino.Input.Custom.GetOption()
 +  go.SetCommandPrompt("​ArrowHead length in screen size (pixles) or world size (percentage of arrow lenght)?"​)
 +  go.AddOption("​screen"​)
 +  go.AddOption("​world"​)
 +  go.Get()
 +  if (go.CommandResult() != Rhino.Commands.Result.Success):​
 +    return go.CommandResult()
 +
 +  screenSize = 0
 +  worldSize = 0.0
 +  if (go.Option().EnglishName == "​screen"​):​
 +    gi = Rhino.Input.Custom.GetInteger()
 +    gi.SetLowerLimit(0,​True)
 +    gi.SetCommandPrompt("​Length of arrow head in pixels"​)
 +    gi.Get()
 +    if (gi.CommandResult() != Rhino.Commands.Result.Success):​
 +      return gi.CommandResult()
 +    screenSize = gi.Number()
 +  else:
 +    gi = Rhino.Input.Custom.GetInteger()
 +    gi.SetLowerLimit(0,​ True)
 +    gi.SetUpperLimit(100,​ False)
 +    gi.SetCommandPrompt("​Lenght of arrow head in percentage of total arrow lenght"​)
 +    gi.Get()
 +    if (gi.CommandResult() != Rhino.Commands.Result.Success):​
 +      return gi.CommandResult()
 +    worldSize = gi.Number()/​100.0
 +
 +
 +  # get arrow start and end points
 +  gp = Rhino.Input.Custom.GetPoint()
 +  gp.SetCommandPrompt("​Start of line")
 +  gp.Get()
 +  if (gp.CommandResult() != Rhino.Commands.Result.Success):​
 +    return gp.CommandResult()
 +  ptStart = gp.Point()
 +
 +  gp.SetCommandPrompt("​End of line")
 +  gp.SetBasePoint(ptStart,​ False)
 +  gp.DrawLineFromPoint(ptStart,​ True)
 +  gp.Get()
 +  if (gp.CommandResult() != Rhino.Commands.Result.Success):​
 +    return gp.CommandResult()
 +  ptEnd = gp.Point()
 +
 +
 +  v = ptEnd - ptStart
 +  if (v.IsTiny(Rhino.RhinoMath.ZeroTolerance)):​
 +    return Rhino.Commands.Result.Nothing
 +
 +  line = Rhino.Geometry.Line(ptStart,​ ptEnd)
 +
 +  conduit = DrawArrowHeadsConduit(line,​ screenSize, worldSize)
 +  conduit.Enabled = True
 +  scriptcontext.doc.Views.Redraw()
 +  rs.GetString("​Pausing for user input"​)
 +  conduit.Enabled = False
 +  scriptcontext.doc.Views.Redraw()
 +
 +  return Rhino.Commands.Result.Success
 +
 +if __name__=="​__main__":​
 +  RunCommand()
 +</​code>​
 +
 +{{tag>​Developer rhinocommon}}
  
developer/rhinocommonsamples/conduitarrowheads.txt ยท Last modified: 2015/09/14 (external edit)