GHPython: 寻找Grasshopper中线和rhino中的brep的交点

我想找一个在GHPython中的线和Rhino中的brep的交点,运行没有错误,可以输出点的GUID,但是Rhino中没有这个点,如何在Rhino中生成这个点?
代码如下
‘’‘
import rhinoscriptsyntax as rs

inputs

center = [3, 0, 0]
end_point = [3, 0, 20]
lines = rs.AddLine(center,end_point)

get intersection points

points = rs.CurveBrepIntersect(lines, brep)
print(points)
’‘’

你写代码没看帮助文档么?CurveBrepIntersect函数返回值是一个列表。你要不切片处理一下,要不把你现在的输出端变成树形数据(也可以在现在points输出端后面再接一个电池,电池就写a=x也会变成树形数据)

谢谢提醒,这个问题已解决,但是出现了一个新的问题。
我将代码扩展了一下:

  1. 如果line和brep有交点,生成交点。这一步没有问题了
  2. 如果line和brep没有交点,则寻找line在末端的延长线和brep的交点,这一步出错。
    这个代码在EditRhinoScript中是运行成功的,line和brep通过get输入。不知道为什么在Grasshopper中却运行失败。
    另外在Grasshopper中的Python怎么逐步运行来debug? EditRhinoScript中可以逐步运行但是Grasshopper中的Python好像不行。

‘’‘
import rhinoscriptsyntax as rs
import Rhino.Geometry as rg

def line_brep_intersection(line, brep):
# find the intersection between the line and the Brep
intersection_events = rs.CurveBrepIntersect(line, brep, tolerance=0.001)

# check if there are any intersection points
if intersection_events is not None:
    if intersection_events[0] is not None:
        intersection_pt = intersection_events[1][0] # return the first intersection pt
else:
    # there are no intersections, extend the line and check for intersections again
    brep = [brep]
    extended_line = rs.ExtendCurve( line, 0, 1, brep)
    # check if there are any extension line
    if extended_line is not None:
        brep = brep[0]
        intersection_events = rs.CurveBrepIntersect(extended_line, brep, tolerance=0.001)
        # check if extension line has any intersection points with brep
        if intersection_events is not None:
            if intersection_events[0] is not None:
                intersection_pt = intersection_events[1][0] # return the first intersection pt
            else:
                intersection_pt = rg.Point3d.Unset # return the first intersection pt
        else:
            intersection_pt = rg.Point3d.Unset # assign an empty value to intersection pt
    else:
        intersection_pt = rg.Point3d.Unset # assign an empty value to intersection pt
return intersection_pt

Define input lines

center = [50, 0, 0]
end_pt = [50, 0, 50]
line = rs.AddLine(center, end_pt)

intersection_pt = line_brep_intersection(line, brep)
incident_line = rs.AddLine(center,intersection_pt)
’‘’

你的代码报错提示是空值报错。我复制了你的代码,在我这里可以运行。你再试试。


我稍微修改了一下你的代码。

import rhinoscriptsyntax as rs
import Rhino.Geometry as rg

def line_brep_intersection(line, brep):
    if(line==None or brep==None):return rg.Point3d.Unset
    #如果成功, 第一个返回的参数是相交曲线,第二个是相交点
    intersection_events = rs.CurveBrepIntersect(line, brep, tolerance=0.001)
    if(intersection_events==None):
        extended_line = rs.ExtendCurve( line, 0, 1, [brep])
        if (extended_line == None):return rg.Point3d.Unset
        intersection_events = rs.CurveBrepIntersect(line, brep, tolerance=0.001)
    if(intersection_events==None):
        return rg.Point3d.Unset
    else:
        return intersection_events[1][0]

center = [50, 0, 0]
end_pt = [50, 0, 50]
line = rs.AddLine(center, end_pt)

intersection_pt = line_brep_intersection(line, brep)
incident_line = rs.AddLine(center,intersection_pt)


曲线和多重曲面相交.gh (9.4 KB)

关于第二个问题,只能自己配置第三方编译器,ghpython自带的这个没办法debug。

2 个赞

十分感谢!你的代码简洁很多!
我发现我的代码不能运行是因为Brep的Type hint,我选的是Brep, 你选的是


当用rhinoscripsyntax时,Type hint都要选这个吗?

只调用rhinoscripsyntax,基本上默认类型就行。