Could not cast GeometryBase in PointCloudItem

Hello,
I am trying to find the closest point to a curvefrom a collection of points using the Curve.ClosestPoints method as given in the RhinoCommon API library, i.e.
Curve.ClosestPoints Method (IEnumerable, Point3d, Point3d, Int32)

Using the following code:
import Rhino.Geometry as rg
from System.Collections.Generic import List

centerPointCloud = rg.PointCloud(iPoints)
geomBaseList = Listrg.GeometryBase

for i in range(0,len(iLines)-1):
par = iLines[i].ClosestPoints(centerPointCloud)

I get an error:
Runtime error (InvalidCastException): Error in IEnumeratorOfTWrapper.Current. Could not cast: Rhino.Geometry.GeometryBase in Rhino.Geometry.PointCloudItem

Traceback:
line 6, in script

I am not able to identify as to where I am making the mistake, have gone through all the discussion topics and still haven’t been able to solve this.

Kindly help,
Thanks

Hi @pinaki.mohanty91,

PointCloudItem does not inherit from GeometryBase, which is why you get the cast exception.

– Dale

Hello @Dale,
But the API documentation says so

Or am I missing something? (I have just recently started learning Python programming)

Hi @,

Yes, PointCloud does inherit from GeometryBase. But if you read the exception closely

Runtime error (InvalidCastException): Error in IEnumeratorOfTWrapper.Current. 
Could not cast: Rhino.Geometry.GeometryBase in Rhino.Geometry.PointCloudItem

PointCloudItem does not inherit from GeometryBase.

Let me know if the attached is what you are looking for.

test_curve_closestpoint.gh (4.9 KB)

– Dale

Hi @dale ,
Thanks again for your reply. The script that you posted finds the parameter of closest point on the line. However, I want to find the point itself which is closest to the line among all the points, and not just the parameter on line. I believe I can do it by looping over the distances from each of the points and find the minimum. However, I was trying to avoid that by using the method

.
In this method I assumed that I could simply pass the collection of points as a point cloud and retrieve the closest point directly. This is when I am getting the error. I am attaching the gh file.test_curve_closestpoints_R1.gh (4.7 KB)

So if it’s a pointcloud that I need to pass, how do I avoid the PointCloudItem . Or is it that I have completely misunderstood the Method ?

I see; I didn’t gleam that from your original post.

This should to the trick:

test_curve_closestpoint.gh (6.1 KB)

Also, Curve.ClosestPoints isn’t going to do what you want, at least without some work. It’s quicker to just to what I demonstrate in the attached.

– Dale

Yes this is what I ended up doing too. Looping through all the distances. Seems I was just trying to save couple of lines in a code. Thanks for your help!