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
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.
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.
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 ?
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!