Site Tools


Differences

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

Link to this comparison view

developer:rhinocommonsamples:customgeometryfilter [2015/09/14] (current)
Line 1: Line 1:
 +===== Sample: Create a specialized GetObject with a Custom Geometry Filter =====
 +
 +===== C# =====
 +<code c#>
 +[System.Runtime.InteropServices.Guid("​5EB284F1-60E6-420A-AE53-6D99732AAE1D"​)]
 +public class CustomGeometryFilterCommand : Command
 +{
 +  private double _tolerance;
 +  public override string EnglishName { get { return "​csCustomGeometryFilter";​ } }
 +
 +  protected override Result RunCommand(RhinoDoc doc, RunMode mode)
 +  {
 +    _tolerance = doc.ModelAbsoluteTolerance;​
 +    ​
 +    // only use a custom geometry filter if no simpler filter does the job
 +
 +    // only curves
 +    var gc = new GetObject();​
 +    gc.SetCommandPrompt("​select curve"​);​
 +    gc.GeometryFilter = ObjectType.Curve;​
 +    gc.DisablePreSelect();​
 +    gc.SubObjectSelect = false;
 +    gc.Get();
 +    if (gc.CommandResult() != Result.Success)
 +      return gc.CommandResult();​
 +    if (null == gc.Object(0).Curve())
 +      return Result.Failure;​
 +    Rhino.RhinoApp.WriteLine("​curve was selected"​);​
 +
 +    // only closed curves
 +    var gcc = new GetObject();​
 +    gcc.SetCommandPrompt("​select closed curve"​);​
 +    gcc.GeometryFilter = ObjectType.Curve;​
 +    gcc.GeometryAttributeFilter = GeometryAttributeFilter.ClosedCurve;​
 +    gcc.DisablePreSelect();​
 +    gcc.SubObjectSelect = false;
 +    gcc.Get();
 +    if (gcc.CommandResult() != Result.Success)
 +      return gcc.CommandResult();​
 +    if (null == gcc.Object(0).Curve())
 +      return Result.Failure;​
 +    Rhino.RhinoApp.WriteLine("​closed curve was selected"​);​
 +
 +    // only circles with a radius of 10
 +    var gcc10 = new GetObject();​
 +    gcc10.SetCommandPrompt("​select circle with radius of 10");
 +    gc.GeometryFilter = ObjectType.Curve;​
 +    gcc10.SetCustomGeometryFilter(CircleWithRadiusOf10GeometryFilter);​ // custom geometry filter
 +    gcc10.DisablePreSelect();​
 +    gcc10.SubObjectSelect = false;
 +    gcc10.Get();​
 +    if (gcc10.CommandResult() != Result.Success)
 +      return gcc10.CommandResult();​
 +    if (null == gcc10.Object(0).Curve())
 +      return Result.Failure;​
 +    Rhino.RhinoApp.WriteLine("​circle with radius of 10 was selected"​);​
 +
 +    return Result.Success;​
 +  }
 +
 +  private bool CircleWithRadiusOf10GeometryFilter (Rhino.DocObjects.RhinoObject rhObject, GeometryBase geometry,
 +    ComponentIndex componentIndex)
 +  {
 +    bool isCircleWithRadiusOf10 = false;
 +    Circle circle;
 +    if (geometry is Curve && (geometry as Curve).TryGetCircle(out circle))
 +      isCircleWithRadiusOf10 = circle.Radius <= 10.0 + _tolerance && circle.Radius >= 10.0 - _tolerance;
 +    return isCircleWithRadiusOf10;​
 +  }
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +<​System.Runtime.InteropServices.Guid("​360ED369-3CE1-47A1-A77E-7CF690CB0786"​)>​ _
 +Public Class CustomGeometryFilterCommand
 +  Inherits Command
 +  Private _tolerance As Double
 +  Public Overrides ReadOnly Property EnglishName() As String
 +    Get
 +      Return "​vbCustomGeometryFilter"​
 +    End Get
 +  End Property
 +
 +  Protected Overrides Function RunCommand(doc As RhinoDoc, mode As RunMode) As Result
 +    _tolerance = doc.ModelAbsoluteTolerance
 +
 +    ' only use a custom geometry filter if no simpler filter does the job
 +
 +    ' only curves
 +    Dim gc = New GetObject()
 +    gc.SetCommandPrompt("​select curve"​)
 +    gc.GeometryFilter = ObjectType.Curve
 +    gc.DisablePreSelect()
 +    gc.SubObjectSelect = False
 +    gc.[Get]()
 +    If gc.CommandResult() <> Result.Success Then
 +      Return gc.CommandResult()
 +    End If
 +    If gc.[Object](0).Curve() Is Nothing Then
 +      Return Result.Failure
 +    End If
 +    Rhino.RhinoApp.WriteLine("​curve was selected"​)
 +
 +    ' only closed curves
 +    Dim gcc = New GetObject()
 +    gcc.SetCommandPrompt("​select closed curve"​)
 +    gcc.GeometryFilter = ObjectType.Curve
 +    gcc.GeometryAttributeFilter = GeometryAttributeFilter.ClosedCurve
 +    gcc.DisablePreSelect()
 +    gcc.SubObjectSelect = False
 +    gcc.[Get]()
 +    If gcc.CommandResult() <> Result.Success Then
 +      Return gcc.CommandResult()
 +    End If
 +    If gcc.[Object](0).Curve() Is Nothing Then
 +      Return Result.Failure
 +    End If
 +    Rhino.RhinoApp.WriteLine("​closed curve was selected"​)
 +
 +    ' only circles with a radius of 10
 +    Dim gcc10 = New GetObject()
 +    gcc10.SetCommandPrompt("​select circle with radius of 10")
 +    gc.GeometryFilter = ObjectType.Curve
 +    gcc10.SetCustomGeometryFilter(AddressOf CircleWithRadiusOf10GeometryFilter)
 +    ' custom geometry filter
 +    gcc10.DisablePreSelect()
 +    gcc10.SubObjectSelect = False
 +    gcc10.[Get]()
 +    If gcc10.CommandResult() <> Result.Success Then
 +      Return gcc10.CommandResult()
 +    End If
 +    If gcc10.[Object](0).Curve() Is Nothing Then
 +      Return Result.Failure
 +    End If
 +    Rhino.RhinoApp.WriteLine("​circle with radius of 10 was selected"​)
 +
 +    Return Result.Success
 +  End Function
 +
 +  Private Function CircleWithRadiusOf10GeometryFilter(rhObject As Rhino.DocObjects.RhinoObject,​ geometry As GeometryBase,​ componentIndex As ComponentIndex) As Boolean
 +    Dim isCircleWithRadiusOf10 As Boolean = False
 +    Dim circle As Circle
 +    If TypeOf geometry Is Curve AndAlso TryCast(geometry,​ Curve).TryGetCircle(circle) Then
 +      isCircleWithRadiusOf10 = circle.Radius <= 10.0 + _tolerance AndAlso circle.Radius >= 10.0 - _tolerance
 +    End If
 +    Return isCircleWithRadiusOf10
 +  End Function
 +End Class
 +d Namespace
 +</​code>​
 +===== Python =====
 +<code python>
 +import rhinoscriptsyntax as rs
 +from scriptcontext import *
 +import Rhino
 +
 +def circleWithRadiusOf10GeometryFilter (rhObject, geometry, componentIndex):​
 +  isCircleWithRadiusOf10 = False
 +  c = rs.coercecurve(geometry)
 +  if c:
 +    b, circle = c.TryGetCircle()
 +  if b:
 +    isCircleWithRadiusOf10 = circle.Radius <= 10.0 + Rhino.RhinoMath.ZeroTolerance and circle.Radius >= 10.0 - Rhino.RhinoMath.ZeroTolerance
 +  return isCircleWithRadiusOf10
 +
 +def RunCommand():​
 +  # only use a custom geometry filter if no simpler filter does the job
 +
 +  # for curves - only a simple filter is needed
 +  if rs.GetObject("​select curve",​ rs.filter.curve):​ #​Rhino.DocObjects.ObjectType.Curve):​
 +    print "curve vas selected"​
 +
 +  # for circles with a radius of 10 - a custom geometry filter is needed
 +  if rs.GetObject("​select circle with radius of 10", rs.filter.curve,​ False, False, circleWithRadiusOf10GeometryFilter):​
 +      print "​circle with radius of 10 was selected"​
 +
 +if __name__=="​__main__":​
 +  RunCommand()
 +</​code>​
 +
 +{{tag>​Developer rhinocommon}}
  
developer/rhinocommonsamples/customgeometryfilter.txt ยท Last modified: 2015/09/14 (external edit)