Hi All,
I have recently started python in Gh and got stuck. I have a problem where i receive list of point3d at Pts variable and then i need to convert this Pts list to pointcloud and find the nearest point and then draw a line from current point to nearest point. Below is my code.
import rhinoscriptsyntax as rs
import Rhino as rc
def RunCommand(Pts, id0, maxn, maxdist, A):
try:
p = rs.PointCloudPoints(Pts)
p = Pts
polylines = []
i = 0
if p:
while len(p)>0 and i < maxn:
polys = PseudoSteinerTrees2(p, maxdist, maxn, id0)
if(len(polys) >0):
polylines.append(polys)
i += 1
A = polylines
# print(A)
except:
print("Unexpected error:")
raise
def PseudoSteinerTrees2(p, maxdist, maxn, id0):
triLines = []
p0 = 0
p1 = 0
p2 = 0
cp = 0
# print(p)
i = 0
while len(p)>1 and i<maxn:
p0 = p[0 % len(p)]
# print(p0)
p.pop(0 % len(p))
cp = p.PointArrayClosestPoint(p0)
# cp = p.index(p0+1)
p1 = p[cp]
# p1 = p[1]
distance = p0.DistanceTo(p1)
if distance > maxdist:
break
p01 = (p0 + p1) * 0.5
# p.RemoveAt(cp)
triLines.append(rc.Geometry.Polyline([p0,p01,p1]))
p0 = p01
i += 1
return triLines
##
#if "__name__" == "__main__":
# print('running...')
#Runcommand1(x)
RunCommand(Pts, id0, max, maxDist, A)
Pts has data:
[<Rhino.Geometry.Point3d object at 0x000000000000003F [-0.502662831685814,-0.778512045637943,-0.0659786402555083]>,
<Rhino.Geometry.Point3d object at 0x0000000000000040 [0.712929626793102,0.547407993836053,-0.822306199847863]>,
<Rhino.Geometry.Point3d object at 0x0000000000000041 [-0.807991005390878,0.903379673093268,-0.0348241026675442]>,
<Rhino.Geometry.Point3d object at 0x0000000000000042 [0.722341490780162,0.890367296473294,0.944099398303823]>,
<Rhino.Geometry.Point3d object at 0x0000000000000043 [0.73663112695172,-0.974628488521384,0.927980866249642]>,
<Rhino.Geometry.Point3d object at 0x0000000000000044 [-0.795870615074351,0.0201654178184296,-0.96995095348449]>]
Image: