Site Tools


Differences

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

Link to this comparison view

developer:rhinocommonsamples:intersectcurves [2015/09/14] (current)
Line 1: Line 1:
 +===== Sample: Calculate Curve Intersections =====
 +
 +===== C# =====
 +<code c#>
 +public static Rhino.Commands.Result IntersectCurves(Rhino.RhinoDoc doc)
 +{
 +  // Select two curves to intersect
 +  var go = new Rhino.Input.Custom.GetObject();​
 +  go.SetCommandPrompt("​Select two curves"​);​
 +  go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve;​
 +  go.GetMultiple(2,​ 2);
 +  if (go.CommandResult() != Rhino.Commands.Result.Success)
 +    return go.CommandResult();​
 +
 +  // Validate input
 +  var curveA = go.Object(0).Curve();​
 +  var curveB = go.Object(1).Curve();​
 +  if (curveA == null || curveB == null)
 +    return Rhino.Commands.Result.Failure;​
 +
 +  // Calculate the intersection
 +  const double intersection_tolerance = 0.001;
 +  const double overlap_tolerance = 0.0;
 +  var events = Rhino.Geometry.Intersect.Intersection.CurveCurve(curveA,​ curveB, intersection_tolerance,​ overlap_tolerance);​
 +
 +  // Process the results
 +  if (events != null)
 +  {
 +    for (int i = 0; i < events.Count;​ i++)
 +    {
 +      var ccx_event = events[i];
 +      doc.Objects.AddPoint(ccx_event.PointA);​
 +      if (ccx_event.PointA.DistanceTo(ccx_event.PointB) > double.Epsilon)
 +      {
 +        doc.Objects.AddPoint(ccx_event.PointB);​
 +        doc.Objects.AddLine(ccx_event.PointA,​ ccx_event.PointB);​
 +      }
 +    }
 +    doc.Views.Redraw();​
 +  }
 +  return Rhino.Commands.Result.Success;​
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +Public Shared Function IntersectCurves(doc As Rhino.RhinoDoc) As Rhino.Commands.Result
 +  ' Select two curves to intersect
 +  Dim go = New Rhino.Input.Custom.GetObject()
 +  go.SetCommandPrompt("​Select two curves"​)
 +  go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve
 +  go.GetMultiple(2,​ 2)
 +  If go.CommandResult() <> Rhino.Commands.Result.Success Then
 +    Return go.CommandResult()
 +  End If
 +
 +  ' Validate input
 +  Dim curveA = go.[Object](0).Curve()
 +  Dim curveB = go.[Object](1).Curve()
 +  If curveA Is Nothing OrElse curveB Is Nothing Then
 +    Return Rhino.Commands.Result.Failure
 +  End If
 +
 +  ' Calculate the intersection
 +  Const intersection_tolerance As Double = 0.001
 +  Const overlap_tolerance As Double = 0.0
 +  Dim events = Rhino.Geometry.Intersect.Intersection.CurveCurve(curveA,​ curveB, intersection_tolerance,​ overlap_tolerance)
 +
 +  ' Process the results
 +  If events IsNot Nothing Then
 +    For i As Integer = 0 To events.Count - 1
 +      Dim ccx_event = events(i)
 +      doc.Objects.AddPoint(ccx_event.PointA)
 +      If ccx_event.PointA.DistanceTo(ccx_event.PointB) > Double.Epsilon Then
 +        doc.Objects.AddPoint(ccx_event.PointB)
 +        doc.Objects.AddLine(ccx_event.PointA,​ ccx_event.PointB)
 +      End If
 +    Next
 +    doc.Views.Redraw()
 +  End If
 +  Return Rhino.Commands.Result.Success
 +End Function
 +</​code>​
 +===== Python =====
 +<code python>
 +import Rhino
 +import scriptcontext
 +
 +def IntersectCurves():​
 +    # Select two curves to intersect
 +    go = Rhino.Input.Custom.GetObject()
 +    go.SetCommandPrompt("​Select two curves"​)
 +    go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve
 +    go.GetMultiple(2,​ 2)
 +    if go.CommandResult()!=Rhino.Commands.Result.Success:​ return
 +
 +    # Validate input
 +    curveA = go.Object(0).Curve()
 +    curveB = go.Object(1).Curve()
 +    if not curveA or not curveB: return
 +
 +    # Calculate the intersection
 +    intersection_tolerance = 0.001
 +    overlap_tolerance = 0.0
 +    events = Rhino.Geometry.Intersect.Intersection.CurveCurve(curveA,​ curveB, intersection_tolerance,​ overlap_tolerance)
 +
 +    # Process the results
 +    if not events: return
 +    for ccx_event in events:
 +        scriptcontext.doc.Objects.AddPoint(ccx_event.PointA)
 +        if ccx_event.PointA.DistanceTo(ccx_event.PointB) > float.Epsilon:​
 +            scriptcontext.doc.Objects.AddPoint(ccx_event.PointB)
 +            scriptcontext.doc.Objects.AddLine(ccx_event.PointA,​ ccx_event.PointB)
 +    scriptcontext.doc.Views.Redraw()
 +
 +if __name__=="​__main__":​
 +    IntersectCurves()
 +</​code>​
 +
 +{{tag>​Developer rhinocommon}}
  
developer/rhinocommonsamples/intersectcurves.txt ยท Last modified: 2015/09/14 (external edit)