Site Tools


Differences

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

Link to this comparison view

developer:rhinocommonsamples:curvesurfaceintersect [2015/09/14] (current)
Line 1: Line 1:
 +===== Sample: Curve-Surface Intersection =====
 +
 +===== C# =====
 +<code c#>
 +[System.Runtime.InteropServices.Guid("​D3E509D4-3791-42C1-A136-FFBA37359290"​)]
 +public class CurveSurfaceIntersectCommand : Command
 +{
 +  public override string EnglishName { get { return "​csCurveSurfaceIntersect";​ } }
 +
 +  protected override Result RunCommand(RhinoDoc doc, RunMode mode)
 +  {
 +    var gs = new GetObject();​
 +    gs.SetCommandPrompt("​select brep"​);​
 +    gs.GeometryFilter = ObjectType.Brep;​
 +    gs.DisablePreSelect();​
 +    gs.SubObjectSelect = false;
 +    gs.Get();
 +    if (gs.CommandResult() != Result.Success)
 +      return gs.CommandResult();​
 +    var brep = gs.Object(0).Brep();​
 +
 +    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();​
 +    var curve = gc.Object(0).Curve();​
 +
 +    if (brep == null || curve == null)
 +      return Result.Failure;​
 +
 +    var tolerance = doc.ModelAbsoluteTolerance;​
 +
 +    Point3d[] intersectionPoints;​
 +    Curve[] overlapCurves;​
 +    if (!Intersection.CurveBrep(curve,​ brep, tolerance, out overlapCurves,​ out intersectionPoints))
 +    {
 +      RhinoApp.WriteLine("​curve brep intersection failed"​);​
 +      return Result.Nothing;​
 +    }
 +
 +    foreach (var overlapCurve in overlapCurves)
 +      doc.Objects.AddCurve(overlapCurve);​
 +    foreach (var intersectionPoint in intersectionPoints)
 +      doc.Objects.AddPoint(intersectionPoint);​
 +
 +    RhinoApp.WriteLine(string.Format("​{0} overlap curves, and {1} intersection points",​ overlapCurves.Length,​ intersectionPoints.Length));​
 +    doc.Views.Redraw();​
 +
 +    return Result.Success;​
 +  }
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +<​System.Runtime.InteropServices.Guid("​926342E4-A665-4932-9E41-BD25F4A12F35"​)>​ _
 +Public Class CurveSurfaceIntersectCommand
 +  Inherits Command
 +  Public Overrides ReadOnly Property EnglishName() As String
 +    Get
 +      Return "​vbCurveSurfaceIntersect"​
 +    End Get
 +  End Property
 +
 +  Protected Overrides Function RunCommand(doc As RhinoDoc, mode As RunMode) As Result
 +    Dim gs = New GetObject()
 +    gs.SetCommandPrompt("​select brep")
 +    gs.GeometryFilter = ObjectType.Brep
 +    gs.DisablePreSelect()
 +    gs.SubObjectSelect = False
 +    gs.[Get]()
 +    If gs.CommandResult() <> Result.Success Then
 +      Return gs.CommandResult()
 +    End If
 +    Dim brep = gs.[Object](0).Brep()
 +
 +    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
 +    Dim curve = gc.[Object](0).Curve()
 +
 +    If brep Is Nothing OrElse curve Is Nothing Then
 +      Return Result.Failure
 +    End If
 +
 +    Dim tolerance = doc.ModelAbsoluteTolerance
 +
 +    Dim intersectionPoints As Point3d()
 +    Dim overlapCurves As Curve()
 +    If Not Intersection.CurveBrep(curve,​ brep, tolerance, overlapCurves,​ intersectionPoints) Then
 +      RhinoApp.WriteLine("​curve brep intersection failed"​)
 +      Return Result.[Nothing]
 +    End If
 +
 +    For Each overlapCurve As Curve In overlapCurves
 +      doc.Objects.AddCurve(overlapCurve)
 +    Next
 +    For Each intersectionPoint As Point3d In intersectionPoints
 +      doc.Objects.AddPoint(intersectionPoint)
 +    Next
 +
 +    RhinoApp.WriteLine(String.Format("​{0} overlap curves, and {1} intersection points",​ overlapCurves.Length,​ intersectionPoints.Length))
 +    doc.Views.Redraw()
 +
 +    Return Result.Success
 +  End Function
 +End Class
 +d Namespace
 +</​code>​
 +===== Python =====
 +<code python>
 +import rhinoscriptsyntax as rs
 +from scriptcontext import *
 +import Rhino
 +import System.Collections.Generic as scg
 +import System as s
 +
 +def RunCommand():​
 +  srfid = rs.GetObject("​select surface",​ rs.filter.surface | rs.filter.polysurface)
 +  if not srfid: return
 + 
 +  crvid = rs.GetObject("​select curve",​ rs.filter.curve)
 +  if not crvid: return
 +
 +  result = rs.CurveBrepIntersect(crvid,​ srfid)
 +  if result == None:
 +    print "no intersection"​
 +    return
 +  ​
 +  curves, points = result
 +  for curve in curves:
 +    doc.Objects.AddCurve(rs.coercecurve(curve))
 +  for point in points:
 +    rs.AddPoint(point)
 +
 +  doc.Views.Redraw()
 +
 +if __name__ == "​__main__":​
 +  RunCommand()
 +</​code>​
 +
 +{{tag>​Developer rhinocommon}}
  
developer/rhinocommonsamples/curvesurfaceintersect.txt ยท Last modified: 2015/09/14 (external edit)